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()?
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 ]
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Mar 04, 2005
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.
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:
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