Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Random Numbers

Eric Christensen
Greenhorn
Posts: 15
I'm writing a simple program with a 'for' loop. I want the first line of this 'for' loop to generate a random number. So if the loop runs 25 times, I want to have a new random number for each iteration. What's the best way to accomplish this?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
See the Random class or therandom() method in Math. From the doc, it looks as though the random() method just creates an internal Random object.
The seed for Random is interesting. Given the same seed, two instances will produce the same sequence of numbers. So you can make a reproducible sequence of numbers with good random distribution. Might be useful for regression testing.

Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
Howdy -- perhaps something like:
int r = 0;
for (i = 0; i < 25; i++) {
r = (int) (Math.random() * 100);
// multiply by 100 to get a number between 0 and 99
// be sure to put parens around the whole thing
}
the random() method is considered a 'pseudo-random' number generator, but it's great for most things.
cheers,
Kathy

Eric Christensen
Greenhorn
Posts: 15
Thanks for the responses.
Kathy, what exactly do you mean by 'pseudo random number generator'?

Layne Lund
Ranch Hand
Posts: 3061
"Pseudo" means "fake". In this context, the numbers generated by Math.random() are not truly random. What is "truly random" then? Well, that's a good question that I personally will leave to the mathemeticians and statisticians. The Math.random() method is good enough in most cases.
I think the main reason that these numbers aren't really random is because you can repeat the same sequence of supposedly random number just by using the same seed value. This is great for debugging purposes, however, it isn't completely secure since someone else could easily generate the same sequence of numbers. This is an especially critical issue in cryptography and security software. However, as I said earlier, Math.random() should be good enough for your current project.
HTH
Layne

Barry Gaunt
Ranch Hand
Posts: 7729
Any one got a USB geiger-counter?

Joel McNary
Bartender
Posts: 1840
Originally posted by Barry Gaunt:
Any one got a USB geiger-counter?

What does this have to do with the price of eggs in Nigeria?

Jim Yingst
Wanderer
Sheriff
Posts: 18671
What does this have to do with the price of eggs in Nigeria?
Very little. What do eggs in Nigeria have to do with this discussion? However a USB geiger counter could be used to generate a truly random number (by measuring the time delay between two consecutive clicks), as opposed to a pseudo-random number. Though I'm not sure offhand if you could generate an even distribution from this, which is typically what's desired. ("Random" and "evenly distributed" are not the same thing.)
[ April 17, 2003: Message edited by: Jim Yingst ]

Joel McNary
Bartender
Posts: 1840
Ahh...it now becomes clear.
Thanks, Jim. I should have known that you would have come through with an explaination

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Some dedicated encryption machines get random variations in CPU heat, electronic noise or other weird hardware signatures. Beyond what I need fer sure.

Ian Cockcroft
Ranch Hand
Posts: 46
Hi, what if you got a seed from the current system time? Get the seconds from the current time, then you have 60 possible seeds for your random number generator. Is this possible?
Ian

Layne Lund
Ranch Hand
Posts: 3061
Yes, not only is it possible, it is very common for most apps that need random numbers. In fact, I bet the Random class uses the current time as a seed. However, as pointed out by earlier posts, other physical phenomenon are "more random" and provide better seeds for critical applications (such as encryption).

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Get the seconds from the current time, then you have 60 possible seeds for your random number generator
60 possible seeds is way too low for many applications. If you encrypt a message using a random seed, and there are only 60 possible seeds, then all someone has to do to decrypt it is try all 60 possibilities. Assuming they know or can guess the algorithm, which is frequently the case. That may seem a little tedious to a human, but with a decent decryption program, it's nothing.
Math.random() uses new java.util.Random(), which uses System.currentTimeMillis() as a seed - a long value, including milliseconds. This is certainly better than having only 60 keys to check, but not necessarily by much. E.g. if you know approximate time an encryption program was run, you might restrict the possible keys to a few thousand - way, way too low by modern standards. Serious encryption programs use more elaborate ways to generate random keys.