• 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 number question

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic