wood burning stoves*
The moose likes Beginning Java and the fly likes Random Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Numbers" Watch "Random Numbers" New topic
Author

Random Numbers

Eric Christensen
Greenhorn

Joined: Jul 29, 2002
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?


"No matter how hard you try, you cannot outthink a moron."
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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

Joined: Jul 29, 2002
Posts: 15
Thanks for the responses.
Kathy, what exactly do you mean by 'pseudo random number generator'?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Any one got a USB geiger-counter?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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?


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
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 ]

"I'm not back." - Bill Harding, Twister
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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

Joined: Jan 29, 2003
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

Joined: Apr 05, 2001
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


- Make it idiot proof and they'll make a better idiot!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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

Joined: Jan 30, 2000
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Numbers
 
Similar Threads
Math.Random() query
Random Number Generator
How do you print a character a random number of times?
Confused With Multi Threading
random numbers