Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random number question

 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tina Long
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic