-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRandomUsedOnce.qhelp
More file actions
57 lines (43 loc) · 2.04 KB
/
RandomUsedOnce.qhelp
File metadata and controls
57 lines (43 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A program that uses <code>java.util.Random</code> to generate a sequence of pseudo-random numbers <em>should not</em> create a new instance of <code>Random</code>
every time a new pseudo-random number is required (for example, <code>new Random().nextInt()</code>).
</p>
<p>
According to the Java API Specification:</p>
<blockquote>
<p>If two instances of <code>Random</code> are created with the same seed, and the same sequence of method calls is made for each, they will generate and return
identical sequences of numbers.</p>
</blockquote>
<p>The sequence of pseudo-random numbers returned by these calls depends only on the value of the seed.
If you construct a new <code>Random</code> object each time a pseudo-random number is needed, this does not
generate a good distribution of pseudo-random numbers, even though the parameterless <code>Random()</code>
constructor tries to initialize itself with a unique seed.
</p>
</overview>
<recommendation>
<p>
Create a <code>Random</code> object once and use the same instance when generating sequences of
pseudo-random numbers (by calling <code>nextInt</code>, <code>nextLong</code>, and so on).
</p>
</recommendation>
<example>
<p>In the following example, generating a series of pseudo-random numbers, such as <code>notReallyRandom</code>
and <code>notReallyRandom2</code>, by creating a new instance of <code>Random</code> each time is
unlikely to result in a good distribution of pseudo-random numbers. In contrast, generating a series
of pseudo-random numbers, such as <code>random1</code> and <code>random2</code>, by calling
<code>nextInt</code> each time <em>is</em> likely to result in a good distribution. This is because
the numbers are based on only one <code>Random</code> object.</p>
<sample src="RandomUsedOnce.java" />
</example>
<references>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html">Random</a>.
</li>
</references>
</qhelp>