Forums Register Login

Random Numbers

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks for the responses.
Kathy, what exactly do you mean by 'pseudo random number generator'?
+Pie Number of slices to send: Send
"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
+Pie Number of slices to send: Send
Any one got a USB geiger-counter?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Ahh...it now becomes clear.
Thanks, Jim. I should have known that you would have come through with an explaination
+Pie Number of slices to send: Send
Some dedicated encryption machines get random variations in CPU heat, electronic noise or other weird hardware signatures. Beyond what I need fer sure.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1487 times.
Similar Threads
random numbers
How do you print a character a random number of times?
Math.Random() query
Random Number Generator
Confused With Multi Threading
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 19:59:44.