| Author |
random number question
|
Tina Long
Ranch Hand
Joined: Mar 04, 2005
Posts: 36
|
|
I need create an unique number that is going to be used in my program. I was looking at the documentation for the java.lang.Math random() - and I wanted to know how it came up with the number it comes up with - does anyone know how it generates it's number? I need to make sure that there is no chance that the same number is generated. I planned on multiplying the random number generated by 10000 and then rounding it so it's an integer. Is there a better way to do this other than using the random()? Thanks!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24056
|
|
|
random() uses the java.util.Random class. The Javadoc for Random documents the algorithm used, and the Random class has a nextInt() method that returns random integers.
|
[Jess in Action][AskingGoodQuestions]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
i'm not sure i quite follow your question. do you mean 1) the program will be run many times. each time the program is run, you need a new number different than all the other numbers generated in all the other runs of the program 2) Within a single run of the program, you will need to generate several numbers, each different from the others. When the program dies, all values are forgotten. When the program is run again, the values generated before can be used again. 3) No matter how many times within a single run of hte program a number is generated, and no matter how many times the program is run, you NEVER want to see the same number pop up a second time. if the program stops and is restarted, the values can be re-used. 4) each run of the program will require 1 random number to be generated. If the program stops and is restarted, the same number can be used again 5) some other thing i didn't think of... if 4 is correct, then i think Math.random() will work just fine. Otherwise, you're gonna need to do a little more, depending on which situation your talking about. [ June 15, 2005: Message edited by: fred rosenberger ]
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Tina Long
Ranch Hand
Joined: Mar 04, 2005
Posts: 36
|
|
well each run of the program saves a file - and in the file there are numbers...i need to ensure that each time the file is saved the same number is not in that file. I think my best bet is just to look at the file - find the highest number and add one. It will work unless the numbers on the page go over 99999 - which is unlikely - I think.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Distributed or clustered systems often use compound keys. Get the high order part from a central key vendor at startup, increment the second part from 1 to some limit, then get another high order part from the vendor. This is preferable to the performance hit of going to the key vendor for every new number. My current project uses keys like this: 12345-12345-123 The first 10 digits come from a database sequence number, the last three are incremented from 001 to 999 within the server. Every thousand keys or so we go to the database for a new key. If we use up the 2 billion range of an int for the first ten digits, I'll change the punctuation, maybe 1234-123456-123. You could use a file or any other persistent resource instead of a database. [ June 16, 2005: Message edited by: Stan James ]
|
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
|
 |
 |
|
|
subject: random number question
|
|
|