tags:

views:

298

answers:

6

I want the Random to return the same values each time. So I tried giving it a const seed. But it still returns random values. How can I stop this?

EDIT: I use the same Random object all over my code, The first time I tested my program I got the following values from the 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 couple of values are quite similar but the rest. Well, you can see

I use private static Random r = new Random(SEED); // The seed is never changed after this point I also use this random to randomize byte arrays. The thing is, I'm getting different outputs for the same input. I wish I could post more code but it's a really big project to post here.

A: 

If you want the same value each time, why not just return a fixed int that is set by a single call to a Random?

Finbarr
@Finbarr: he said "values", not "value" and from it's example it seems pretty obvious is after not a single value but a repeatable sequence of pseudo-random **values**
Webinator
His first posting was ambiguous. He has since clarified what he is looking for.
Finbarr
+3  A: 

The following snippet of code should always produce 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));
    }
}

Output:

3
7
7
5

To go back to "real" random behavior, simply remove the argument to the constructor (and it will pick a "random" seed).


For more information, see the API-docs for Random:

Random

public Random(long seed)

Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

The invocation 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) to return the same number every 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));
aioobe
+2  A: 

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

According to the 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, they will generate and return identical sequences of numbers.

Derek Clarkson
A: 

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 is, as far as I know, the technique used by Doom to lower the transmission of data during network games. All players had the same table of random numbers that would be used to calculate damage, so the actual damage data was not transmitted on the network.

There is an story on the Daily WTF regarding an application where there was the need to actually have a "randomized" list which would be used always:

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

Mario Ortegón
+6  A: 

I can only see two ways in which this can happen:

  1. The two executions don't share precisely the same order of invocation. There's something you're missing - e.g. some method invocation on the random object from some other thread or something. As PRNGs are completely deterministic, this is the most likely thing - I'm sure you already checked it but it still seems the most likely explanation.

  2. You've found a bug in the PRNG. Unlikely but not impossible - if you provide us with details like Java version, seed used, etc. then maybe we could try and reproduce it.

In any case, I would strongly recommend you create an "as short as possible" toy-application which can replicate the discrepancy, and then update your question with it if the problem persists.

Oak
@Oak: +1... You're the only so far that tries to help the OP instead of stupidly quoting the Javadoc / stating the obvious. The OP seems aware that given a constant seed he's supposed to get always the same values, yet people keep quoting the Javadoc to upvote-whore. Sick.
Webinator
+1 for pointing out the most likely scenario (1.). As for 2. ... Looking at the source for juRandom there isn't much space for bugs. Heck, an LCG is almost impossible to get wrong algorithm-wise (the parameters are another matter, entirely).
Joey
Thanks for the suggestion but it's a bug in the code
ComputerJy
+2  A: 

Your two sequences are far from random sequences of integers. There is a lot of repetition and large parts of the sub-sequences equal each other. I believe you have some bug in your 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
aioobe
Well, I tried printing out all values generated from the random and yes, there seems to be something wrong. A boolean is printed where an integer is printed in another execution level. So I think it's the random behavior causing the problem. Thanks
ComputerJy
@ComputerJy: The analysis of aioobe possibly indicates that you use the same Random object for generating other random numbers for other purposes. Consequently, the array does not contain the complete sequence, and it has "holes" in it. Check all the places where you use the same random generator.
Eyal Schneider
its a private static object so it's not called from other classes, these are the nextInt calls, I also have nextBoolean and nextBytes calls, but I thought just the ints would be sufficient. Anyway, the reason in the code causing this is my latest question. Thanks again
ComputerJy
private static object could still be called by other instances of the same class though...
Finbarr