Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Random unique in java

 
Chandra shekar M
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
We have java.util.Random class for Random number generator. I have heard Theories that Random number is not really random that is it may generate
duplicate values is this correct? If yes why?

Thanks
Chandra
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:We have java.util.Random class for Random number generator. I have heard Theories that Random number is not really random that is it may generate
duplicate values is this correct? If yes why?

Of course it will generate duplicate values, and for lots of reasons:
1. You can set the seed, which is a way of predetermining which "random" numbers will be produced - great for testing, BTW.
2. The random number it produces is based on some type - an int, a byte, or a long, which have a finite number of values themselves. Therefore eventually, it is bound to produce a duplicate.
3. The random number generator itself is finite (I believe based on a 48-bit generator).
4. Duplication of scoped random values is subject to the birthday paradox.

And it has nothing to do with "how random" java.util.Random is, which is, for most case pretty darn random. There is however SecureRandom, if you think you really need it; although it may be quite a bit slower.

HIH. At least it may get you reading.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by theories?
Read the documentation for Random, and you find it produces pseudo‑random numbers. No computer or machine can produce true randomness; you would need a radioactive source, thermal noise in a resistor, or similar, for true randomness. There is another class which produces more secure random numbers, but I have forgotten what it is called.
Random numbers are guaranteed to repeat eventually; something truly random has no memory for what has preceded it and you can, for example, have a coin tossed which repeatedly comes up heads. I once won £10 by betting a coin would repeatedly come up heads!
If you mean no repeats, that is called random selection from a declining population or similar, and you can find out about that if you search the Ranch.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you mean no repeats, that is called random selection from a declining population or similar.

and if you do something like this, you eventually will run out of numbers...and then what you do do?

Note: I'm not saying that it is a BAD thing...just that you need to think about it.
 
Chandra shekar M
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks To All
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with random selection from a declining population; it is simply different from random.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:Thanks To All
You’re welcome
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is nothing wrong with random selection from a declining population; it is simply different from random.

I agree 100%. I was trying to get the OP to think about it, because you have to know what do do once you run out, if you do go this way.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:and if you do something like this, you eventually will run out of numbers...and then what you do do?

believe string dont have a bound.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:believe string dont have a bound.

Yes it does; it's just a lot higher. However, if you want a really big number, use BigInteger, which even has its own random number generator.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic