aspose file tools*
The moose likes Java in General and the fly likes Is Random unique in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is Random unique in java" Watch "Is Random unique in java" New topic
Author

Is Random unique in java

Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
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

Joined: Mar 17, 2011
Posts: 7640
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38350
    
  23
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 135
Thanks To All
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38350
    
  23
There is nothing wrong with random selection from a declining population; it is simply different from random.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38350
    
  23
Chandra shekar M wrote:Thanks To All
You’re welcome
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Mar 17, 2011
Posts: 7640
    
  19

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is Random unique in java