wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random numbers" Watch "random numbers" New topic
Author

random numbers

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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 ]
Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41
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."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 04, 2005
Posts: 172
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 04, 2005
Posts: 172
thanks alot Jeff
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 04, 2005
Posts: 172
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 04, 2005
Posts: 172
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!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

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!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Chastened, I return to the Android's Dungeon...
 
jQuery in Action, 2nd edition
 
subject: random numbers
 
Similar Threads
Generating Random Number
generating unique numbers
Creating a unique random number...
Random 5-Digit number
Please help