• 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

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and welcome to my thread

can some one please explain to me the difference between

Random() and
Random(seed) methods?

and can you please tell me how can i generate a unique ID that consists of 5 numbers starting from 10000-20000?
i saw a few in this forum but most of them start from 0!

is it a good idea to have an algorithm that generates numbers between 0 & 10000 and then add 10000 to the generated number? i feel like its not very clever!

thank uuuu
Hannah
[ January 03, 2006: Message edited by: H Melua ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you make two Random-objects with the same seed, they will return the same values.



That code would print 100 times 'true'. But if you make the objects with no parameters, it will print false.

This is what API says about Random()-constructor without parameters:
"Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor."
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the API for the Random constructor has actually changed from JDK 1.4 to JDK 5. Matti gave teh JDK 5 version. The older JDK says:

Creates a new random number generator. Its seed is initialized to a value based on the current time:

    public Random() { this(System.currentTimeMillis()); }

Two Random objects created within the same millisecond will have the same sequence of random numbers.


I would say the new version is better, as the code actually does make it very unlikely that two Randoms created in the same millisecond will get the same value.

Typically you want your random numbers to be as random as possible, meaning you wouldn't want two Random objects to have the same sequence of numbers. However sometimes this is fine. One place I've used Randoms with known seeds is for testing. I may want to simulate a long random sequence of numbers for a test, but I also want the results to be reproducible. So I might create a new Random(0) or new Random(123456) or whatever, and the test will run consistently each time.
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u Matti & Jim

thats my code if it makes sence to any1


i've chosen 90001 to aviod (as much as possible) having two numbers the same(!) specially if i have 10000 items!

comments are welcome
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you switched from java.util.Random to java.lang.Math.random?
Anyhowdy, the code you gave will generate a number in the interval [10,000-100,000],
including the end points. Originally you mentioned a different interval?
Finally, often the best question is what is your goal, what are you trying to do?
In the OP you mentioned in passing something about a unique id.
Why do you need a unique id -- is this something to do with databases?
And if you are using the current version of Java, do you know about java.util.UUID?
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Jeff

yes your right i'm working with databases.. thats y i want unique IDs, and why have i switched from this to that, is because they are 2 slightly separate questions, one is the difference between the random methods since i read about them but didnt understand the difference, and then i explained what i want...

the code you gave will generate a number in the interval [10,000-100,000],including the end points


yes thats what i want

Originally you mentioned a different interval?


yes i did and i mentioed y i decided to change... i'm not worried about the interval, all i'm woried about is "starting from 10000" rather than 0... and remaining 5 numbers

do you know about java.util.UUID



no actually! i'll have a look thanks for that
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you working with a database, does it provide its own key generation?
For example MySQL has auto increment fields and Oracle has sequences?
I don't know if you've clearly defined why you need unique numbers.
If you were to do that, it might make it clear what the best approach would be.
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it does, and i originally done it that way, but i had to change it since i need to know the ID before i can insert it!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by H Melua:
i need to know the ID before i can insert it!



That's usually the case with these ids. In Oracle you can use the SQL:

SELECT YOUR_SEQ_NAME.nextval FROM dual

to get the sequence value.
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks alot Jeff
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using random numbers to generate unique identifiers is not a good idea - random numbers are not unique, they're only random. Sooner or later the same number will be generated twice.

It's best to use an Oracle sequence to generate unique numbers as Jeff shows - if you're not using Oracle, look in the documentation of your database, it most likely has something to generate unique IDs.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a general lesson here, too. The first posts in this thread were just about generating random numbers, but in the end, the true problem the original poster had was about Oracle sequence numbers. These two things have *nothing* to do with each other. Lesson: for posters it's important to know what your question *really* is. Too often, an overly specific question is asked, and answering it doesn't help the poster in a way, because there are asking an irrelevant question. When asking a question, one should be brief, yes, but it's important to give your context and state what you are trying to do, what your goal is.
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper de Jong true, thats why i've chosen intervals [10,000-100,000] just for 10,000 items... less likely that two numbers will be the same

Jeff, i dont see why i need to post two seperate threads for the same purpose! i didnt need to mention my database (you the one who asked about it!), all i wanted to know is how to generate a number! and i'm not working with oracle anyway..., meaning, if u didnt ask me about why do i need it, i wouldnt have mentioned it and it wouldnt have made a difference in my solution...

i think i know the rules of the forum...
thank you
Hannah
[ January 03, 2006: Message edited by: H Melua ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps I was stepping up onto a soapbox there, but what I wrote wasn't directed at you in particular. I think that at least half of the threads in Java in General (beginner) suffer from the inability of the OP to phase their questions properly. Perhaps they don't have a good grasp of the terminology - that is understandable. But just as often, it's that they're asking the wrong question, and that only becomes apparent several posts into the thread as the true goal of the poster is teased out of them.
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely it doesnt apply to me, since i had a solution in my second reply...

in all cases, what you said about others applies to you as well, since 'You' are discussing something OFF-Topic!
 
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
Let's try to not turn this thread into a debate of who's doing what wrong. This looks like it's starting to go down a road it shouldn't. It hasn't gone too far just yet, so let's just let it drop now.

hopefully the OP got the answer to their question. that's all that really matters.

thanks!!!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chastened, I return to the Android's Dungeon...
reply
    Bookmark Topic Watch Topic
  • New Topic