3

I need to test out a Java program 20 times and need to set the random seed so that the tests can be repeated. If I were to set the initial seed as 0 and then increment by 1 at each run (i.e. 1,2,3 etc), would this method still ensure complete randomness, even though the seeds are not far apart?

Thank you

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
badcoder
  • 3,624
  • 5
  • 32
  • 33

4 Answers4

6

Any seed will provide the same level of randomness as any other seed for a standard PRNG like the one included with Java. So it's fine to use an incrementing seed for tests.

You might want to consider using a better random number generator though. The one included with Java yields noticeable repeating patterns if you render the values as an image (I can't find a reference offhand, but I recall it being apparent). I'd recommend the Mersenne Twister (there are Java versions) as an alternative that's fast and has a very long period so you won't easily see patterns.

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • No problem. I updated the answer about how to get better randomness if you're concerned about it. Either way you can use incrementing seeds for your tests, or you could even use a series of random numbers (generated from a fixed seed) to use as seeds for each iteration :) – WhiteFang34 Apr 27 '11 at 04:25
2

If you use a seed of 0 the sequence of random numbers will be repeatable from that point. You would only need to use a different random seed if you wanted a different sequence. i.e. you should be able to set the seed once per test.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Why not just use the current time as the seed? ie, System.currentTimeMillis() ?

ryanprayogo
  • 11,587
  • 11
  • 51
  • 66
  • because i need to repeat the tests – badcoder Apr 27 '11 at 04:04
  • Let me rephrase my answer. If you need to repeat the test 20 times with the same sequence of random numbers, then you will need to use the same seed. Otherwise, you can use the current time as the seed. Perhaps I'm not understanding your question correctly? – ryanprayogo Apr 27 '11 at 04:07
0

Do you need a number or a string? I use UUID.randomUUID().toString() to get a UUID for my tests when I need strings, but if you need a number then you can use System.nanoTime().

lobster1234
  • 7,679
  • 26
  • 30