2

This has stumped me. Why would this return a string of all 1's? I do believe that it has something to do with the seed value. When that is changed the numbers become much more diverse.

Random random = new Random(441287210L);

for (int i = 0; i < 10; i++)
   System.out.print(random.nextInt(10) + " ");

Any ideas?

Sebastian Barth
  • 4,079
  • 7
  • 40
  • 59
StillLearningToCode
  • 2,271
  • 4
  • 27
  • 46
  • 4
    See this link, http://stackoverflow.com/questions/13241937/random-class-acting-odd It just so happens that the seed you're using generates that sequence of numbers, remember the seed is a constant, as is its generated sequence. – ambs Oct 02 '16 at 22:49
  • With the seed `441287210L` it just so happens to produce all ones. With the same seed every time it will always produce the same sequence of numbers, because it is a **pseudo** random number generator. Try the seed `441287211L` (your seed plus one) and it produces `7 1 9 5 8 3 2 7 3 4` - every time. Try `new Random(System.currentTimeMillis());` and try `new Random()` and those will be different every time. – Stephen P Oct 02 '16 at 23:00

2 Answers2

4

If a random algorithm was incapable of generating a sequence that started with 10 consecutive 1's, it wouldn't be a very good random algorithm. Just as long as it only happens for 1/10,000,000,000 seeds. With a 48-bit seed, you should find approximately 28 thousand seeds that do the same thing.

Glenn Lane
  • 3,892
  • 17
  • 31
3

Congratulations, you happened to find the seed that corresponds to a probabilistic 1 in 10 billion occurrence. Given that Java's Random has a cycle of 2**48, there should be around 28 thousand seeds which will produce comparable behaviors.

pjs
  • 18,696
  • 4
  • 27
  • 56