• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random Numbers

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses.
Kathy, what exactly do you mean by 'pseudo random number generator'?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one got a USB geiger-counter?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh...it now becomes clear.
Thanks, Jim. I should have known that you would have come through with an explaination
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some dedicated encryption machines get random variations in CPU heat, electronic noise or other weird hardware signatures. Beyond what I need fer sure.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic