1+ package com .baeldung .threadlocalrandom ;
2+
3+ import java .util .concurrent .ThreadLocalRandom ;
4+
5+ import static org .junit .Assert .assertTrue ;
6+ import org .junit .Test ;
7+
8+ public class ThreadLocalRandomTest {
9+
10+ @ Test
11+ public void givenUsingThreadLocalRandom_whenGeneratingRandomIntBounded_thenCorrect () {
12+ int leftLimit = 1 ;
13+ int rightLimit = 100 ;
14+ int generatedInt = ThreadLocalRandom .current ().nextInt (leftLimit , rightLimit );
15+
16+ assertTrue (generatedInt < rightLimit && generatedInt >= leftLimit );
17+ }
18+
19+ @ Test
20+ public void givenUsingThreadLocalRandom_whenGeneratingRandomIntUnbounded_thenCorrect () {
21+ int generatedInt = ThreadLocalRandom .current ().nextInt ();
22+
23+ assertTrue (generatedInt < Integer .MAX_VALUE && generatedInt >= Integer .MIN_VALUE );
24+ }
25+
26+ @ Test
27+ public void givenUsingThreadLocalRandom_whenGeneratingRandomLongBounded_thenCorrect () {
28+ long leftLimit = 1L ;
29+ long rightLimit = 100L ;
30+ long generatedLong = ThreadLocalRandom .current ().nextLong (leftLimit , rightLimit );
31+
32+ assertTrue (generatedLong < rightLimit && generatedLong >= leftLimit );
33+ }
34+
35+ @ Test
36+ public void givenUsingThreadLocalRandom_whenGeneratingRandomLongUnbounded_thenCorrect () {
37+ long generatedInt = ThreadLocalRandom .current ().nextLong ();
38+
39+ assertTrue (generatedInt < Long .MAX_VALUE && generatedInt >= Long .MIN_VALUE );
40+ }
41+
42+ @ Test
43+ public void givenUsingThreadLocalRandom_whenGeneratingRandomDoubleBounded_thenCorrect () {
44+ double leftLimit = 1D ;
45+ double rightLimit = 100D ;
46+ double generatedInt = ThreadLocalRandom .current ().nextDouble (leftLimit , rightLimit );
47+
48+ assertTrue (generatedInt < rightLimit && generatedInt >= leftLimit );
49+ }
50+
51+ @ Test
52+ public void givenUsingThreadLocalRandom_whenGeneratingRandomDoubleUnbounded_thenCorrect () {
53+ double generatedInt = ThreadLocalRandom .current ().nextDouble ();
54+
55+ assertTrue (generatedInt < Double .MAX_VALUE && generatedInt >= Double .MIN_VALUE );
56+ }
57+
58+ }
0 commit comments