Random Java Values ​​Differ Each Time

I want Random to return the same values ​​every time. So I tried to give it the seed of const. But it still returns random values. How can i stop this?

EDIT: I use the same random object throughout my code. The first time I tested my program, I got the following values ​​from random:

13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 80, 33, 3, 0, 45, 6, 2, 51, 50, 3, 0, 1, 1, 0, 2, 3, 0, 0, 2, 0, 3, 0, 1, 33, 1, 22, 7, 55, 92, 33, 1, 5, 6, 10, 2, 1, 85, 26, 1, 3, 42, 16, 0, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 4, 97, 57, 85, 28, 0, 2, 3, 9, 36, 1, 19, 4, 0, 71, 9, 2, 3, 76, 6, 3, 0, 96, 84, 22, 0, 0, 1, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 1, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 3, 0, 6, 1, 64, 39, 2, 0, 51, 31, 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 80, 33, 3, 0, 45, 6, 2, 51, 50, 3, 0, 1, 1, 0, 2, 3, 0, 0, 2, 0, 3, 0, 1, 33, 1, 22, 7, 55, 92, 33, 1, 5, 6, 10, 2, 1, 85, 26, 1, 3, 42, 16, 0, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 4, 97, 57, 85, 28, 0, 2, 3, 9, 36, 1, 19, 4, 0, 71, 9, 2, 3, 76, 6, 3, 0, 96, 84, 22, 0, 0, 1, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 1, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 3, 0, 6, 1, 64, 39, 2, 0, 51, 31 

The second time I got:

 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 2, 1, 1, 3, 1, 0, 33, 9, 61, 1, 2, 54, 59, 99, 28, 0, 88, 11, 0, 0, 92, 24, 0, 47, 43, 83, 13, 1, 1, 53, 46, 22, 0, 2, 0, 0, 0, 0, 2, 0, 13, 8, 1, 16, 6, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 0, 97, 57, 85, 28, 5, 2, 3, 3, 36, 4, 19, 0, 0, 71, 9, 2, 3, 76, 6, 0, 0, 0, 2, 4, 2, 0, 1, 0, 1, 1, 0, 0, 0, 2, 6, 1, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 0, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 2, 1, 1, 3, 1, 0, 33, 9, 61, 1, 2, 54, 59, 99, 28, 0, 88, 11, 0, 0, 92, 24, 0, 47, 43, 83, 13, 1, 1, 53, 46, 22, 0, 2, 0, 0, 0, 0, 2, 0, 13, 8, 1, 16, 6, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 0, 97, 57, 85, 28, 5, 2, 3, 3, 36, 4, 19, 0, 0, 71, 9, 2, 3, 76, 6, 0, 0, 0, 2, 4, 2, 0, 1, 0, 1, 1, 0, 0, 0, 2, 6, 1, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 0, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0 

The first pair of values ​​is very similar, but everything else. Well you can see

I use private static Random r = new Random (SEED); // Seed never changes after this point I also use this random to randomize byte arrays. The fact is that I get different outputs for one input. I wish I could post more code, but this is a really big project to post here.

+4
source share
7 answers

Your two sequences are far from random sequences of integers. There are many repetitions and large parts of subsequences equal to each other. I believe you have a bug in the code.

Sequence 1:

 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 80, 33, 3, 0, 45, 6, 2, 51, 50, 3, 0, 1, 1, 0, 2, 3, 0, 0, 2, 0, 3, 0, 1, 33, 1, 22, 7, 55, 92, 33, 1, 5, 6, 10, 2, 1, 85, 26, 1, 3, 42, 16, 0, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 4, 97, 57, 85, 28, 0, 2, 3, 9, 36, 1, 19, 4, 0, 71, 9, 2, 3, 76, 6, 3, 0, 96, 84, 22, 0, 0, 1, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 1, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 3, 0, 6, 1, 64, 39, 2, 0, 51, 31, 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, 80, 33, 3, 0, 45, 6, 2, 51, 50, 3, 0, 1, 1, 0, 2, 3, 0, 0, 2, 0, 3, 0, 1, 33, 1, 22, 7, 55, 92, 33, 1, 5, 6, 10, 2, 1, 85, 26, 1, 3, 42, 16, 0, 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 4, 97, 57, 85, 28, 0, 2, 3, 9, 36, 1, 19, 4, 0, 71, 9, 2, 3, 76, 6, 3, 0, 96, 84, 22, 0, 0, 1, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 1, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 3, 0, 6, 1, 64, 39, 2, 0, 51, 31 

Sequence 2:

 // In sync with sequence 1 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, // Out of sync with sequence 1 2, 1, 1, 3, 1, 0, 33, 9, 61, 1, 2, 54, 59, 99, 28, 0, 88, 11, 0, 0, 92, 24, 0, 47, 43, 83, 13, 1, 1, 53, 46, 22, 0, 2, 0, 0, 0, 0, 2, 0, 13, 8, 1, 16, 6, // Back in sync! 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 0, 97, 57, 85, 28, 5, 2, 3, 3, 36, 4, 19, 0, 0, 71, 9, 2, 3, 76, 6, 0, 0, 0, 2, 4, 2, 0, 1, 0, 1, 1, 0, 0, 0, 2, 6, 1, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 0, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0, 13, 9, 10, 12, 14, 11, 15, 10, 8, 6, 12, 9, 7, 7, 6, 1, 0, 0, 0, // Out of sync again... 2, 1, 1, 3, 1, 0, 33, 9, 61, 1, 2, 54, 59, 99, 28, 0, 88, 11, 0, 0, 92, 24, 0, 47, 43, 83, 13, 1, 1, 53, 46, 22, 0, 2, 0, 0, 0, 0, 2, 0, 13, 8, 1, 16, 6, // Back in sync.... 2, 34, 0, 1, 2, 8, 0, 73, 1, 4, 66, 59, 49, 99, 2, 0, 97, 57, 85, 28, 5, 2, 3, 3, 36, 4, 19, 0, 0, 71, 9, 2, 3, 76, 6, 0, 0, 0, 2, 4, 2, 0, 1, 0, 1, 1, 0, 0, 0, 2, 6, 1, 58, 2, 1, 2, 1, 0, 1, 5, 0, 0, 0, 0, 4, 21, 59, 3, 1, 6, 3, 6, 0, 0, 3, 2, 2, 3, 5, 3, 0, 6, 5, 60, 3, 4, 4, 2, 6, 1, 3, 5, 1, 5, 26, 4, 0, 25, 5, 0, 61, 2, 0, 29, 2, 1, 34, 57, 55, 61, 1, 1, 21, 6, 1, 3, 0, 56, 6, 23, 6, 0, 47, 1, 1, 55, 0, 0, 5, 1, 4, 0, 57, 21, 45, 2, 0 
+1
source

I can only see two ways this can happen:

  • Two executions do not have exactly the same call order. There is something missing for you - for example, some method call to a random object from some other thread or something like that. Since PRNGs are completely deterministic, this is the most likely thing - I'm sure you already checked it, but that still seems to be the most likely explanation.

  • You found a bug in PRNG. It is unlikely, but not impossible - if you provide us with details such as the Java version, seed used, etc., Then, perhaps, we can try to reproduce it.

In any case, I highly recommend that you create a "as short as possible" toy application that can replicate the mismatch, and then update your question if the problem persists .

+6
source

The following code snippet should always output the same output:

 import java.util.Random; public class Test { public static void main(String... args) { Random r = new Random(123456L); // use the seed 123456L System.out.println(r.nextInt(10)); System.out.println(r.nextInt(10)); System.out.println(r.nextInt(10)); System.out.println(r.nextInt(10)); } } 

Conclusion:

 3 7 7 5 

To return to "real" random behavior, simply remove the argument to the constructor (and it will select the "random" seed).


For more information, see the API docs for Random :

Random

public Random(long seed)

Creates a new random number generator using one long seed. Seeds are the initial value of the internal state of the pseudo-random number generator, which is supported by the next (int) method.

Calling a new Random (seed) is equivalent to:

 Random rnd = new Random(); rnd.setSeed(seed); 

Parameters:
seed - the initial seed
See also:
setSeed(long)


If you want r.nextInt(10) return the same number for each call, you need to do:

  r.setSeed(123456L); System.out.println(r.nextInt(10)); r.setSeed(123456L); System.out.println(r.nextInt(10)); r.setSeed(123456L); System.out.println(r.nextInt(10)); r.setSeed(123456L); System.out.println(r.nextInt(10)); 
+4
source

Should you use instances of the java.util.Random or java.lang.Math.random () class?

According to javadoc on Random:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each of them, they will generate and return identical sequences of numbers.

+2
source

Using the same seed should produce the same sequence on each platform.

Keep in mind that if you use the same Random object to also make nextBoolean or nextBytes calls in addition to nextInt calls, and they don't occur in the same exact sequence along with nextInt calls that can affect the sequence which you receive in your subsequent calls. In addition, each call to the next one must use the same parameter values ​​or without parameters in the same sequence each time so that the return values ​​are the same.

Each subsequent call to the Random object affects the sequence. In other words, nextInt, nextBoolean, and nextBytes interact and influence the sequence, even if you use a seed. To get completely repeatable results, you must use the same seed with the exact same sequence of subsequent calls.

Below are two pairs of results. The first pair is generating 100 integers from 0 to 99, and then creating a new Random with the same seed and creating another 100 integers from 0 to 99. The whole sequences are identical, which was expected according to the Java documentation.

The second pair is generating 100 integers from 0 to 99, and then creating a new Random with the same seed and generating 100 integers from 0 to 99, except that after every 10 integers, calling the same Random object with alternating calls to nextBoolean and nextBytes. You can see in the results that the sequences are repeatable and that calls to the same random object by the other following methods affect the sequence from nextInt. You can also see that the common pattern in nextInt continues, but at a later point in the sequence. If alternate calls to the same random ojbect other following methods do not happen exactly the same every time, the whole sequence will not be the same.

Two nextInt () calls for 100 integers from 0 to 99 using seeds 123456789L

The first set of 100 of the following queries (100) ...

65 0 83 44 90 54 33 7 97 87

41 61 64 82 24 61 60 89 30 79

42 86 38 54 61 25 98 20 99 88

65 33 91 30 61 64 27 27 74 80 13

58 63 66 38 34 89 46 5 88 79

50 18 5 24 65 63 58 6 73 80

84 5 33 1 58 29 9 87 0 83

1 75 84 81 16 2 8 14 98 26

64 28 40 50 33 70 81 57 71 18

58 37 36 48 71 28 95 77 74 92

The second set of the following 100 queries (100) ...

65 0 83 44 90 54 33 7 97 87

41 61 64 82 24 61 60 89 30 79

42 86 38 54 61 25 98 20 99 88

65 33 91 30 61 64 27 27 74 80 13

58 63 66 38 34 89 46 5 88 79

50 18 5 24 65 63 58 6 73 80

84 5 33 1 58 29 9 87 0 83

1 75 84 81 16 2 8 14 98 26

64 28 40 50 33 70 81 57 71 18

58 37 36 48 71 28 95 77 74 92

As you can see, I get accurate results. Now here's what happens if I insert alternating calls to nextBoolean and nextBytes every 10th call to nextInt () in the second sequence.

Two nextInt () calls for 100 integers from 0 to 99 using seeds 123456789L

The first set of 100 of the following queries (100) ...

65 0 83 44 90 54 33 7 97 87

41 61 64 82 24 61 60 89 30 79

42 86 38 54 61 25 98 20 99 88

65 33 91 30 61 64 27 27 74 80 13

58 63 66 38 34 89 46 5 88 79

50 18 5 24 65 63 58 6 73 80

84 5 33 1 58 29 9 87 0 83

1 75 84 81 16 2 8 14 98 26

64 28 40 50 33 70 81 57 71 18

58 37 36 48 71 28 95 77 74 92

The second set of 100 nextInt (100) calls with alternating calls nextBoolean () and nextBytes (byte [10]) after every 10th call to nextInt (100) ...

65 0 83 44 90 54 33 7 97 87

falsely

61 64 82 24 61 60 89 30 79 42

bytes: -75 -111 34 -128 -92 23 95 -112 100 -76

61 25 98 20 99 88 65 33 91 30

falsely

64 27 74 80 13 58 63 66 38 34

bytes: -118 -57 19 -74 36 -70 31 114 -125 -61

88 79 50 18 5 24 65 63 58 6

falsely

80 84 5 33 1 58 29 9 87 0

bytes: 63 -82 -79 93 -126 2 23 2 -90 122

84 81 16 2 8 14 98 26 64 28

falsely

50 33 70 81 57 71 18 58 37 36

bytes: -8 89 73 71 -25 -104 -28 12 -32 -110

95 77 74 92 92 78 36 36 99 78 61

falsely

66 39 19 18 19 99 41 68 38 48

bytes: 64 -71 126 97 17 -6 -120 94 73 64

Note that in the resulting template, nextBoolean interleaved eats uses one integer value from the nextInt sequence and that nextBytes (byteArray), where byteArray is declared as byte [10], uses three integer values ​​from the nextInt sequence.

0
source

If you need the same value every time, why not just return the fixed int given by one call to Random?

-1
source

Another possibility is that you create your own class and populate it with a list or pre-generated "random" values ​​created using a random number generator. This, as far as I know, is a technique used by Doom to reduce data transfer during network games. All players had the same random number table that would be used to calculate the damage, so the actual damage data was not transferred to the network.

The Daily WTF has a story about an application in which you had to have a "randomized" list that would always be used:

http://thedailywtf.com/Articles/More-or-Less-Random.aspx

-1
source

Source: https://habr.com/ru/post/1309522/


All Articles