0

In java what is the best way to create a truely random number sequence? One that is 100% never to be repeated again

Thank you

6
Contributors
14
Replies
15
Views
4 Years
Discussion Span
Last Post by jalpesh_007
Featured Replies
  • Apparently Intel processors from Ivy Bridge onwards have a hardware random number generator instruction that is as near quantum-mechanical noise random as you can be... http://software.intel.com/sites/default/files/m/d/4/1/d/8/441_Intel_R__DRNG_Software_Implementation_Guide_final_Aug7.pdf Read More

0

Look into the UUID class offered by Java. Though the probability of collision is not 0%, it's still very low. Read this.

Which function to I call from that class to give me a random number? hash, etc.

0

for questions about UUID you should check here
also: if you want it to be 100% ... hard to do, especially without some additional coding from your part. as you probably know, there are collections that don't accept doubles, so try storing all the so-far existing keys in one of those, and see whether or not when you add a generated one, it is added.

0

for questions about UUID you should check here
also: if you want it to be 100% ... hard to do, especially without some additional coding from your part. as you probably know, there are collections that don't accept doubles, so try storing all the so-far existing keys in one of those, and see whether or not when you add a generated one, it is added.

Thats a link to the same page sos put. It tells me absolutly nothing except what functions there are.

I know 100% is impossible but the closest to it.

0

UUID.randomUUID().toString() gives the random-almost-unique string you are looking for. Replace the dashes which are present in the returned string if you don't need them.

0

UUID.randomUUID().toString() gives the random-almost-unique string you are looking for. Replace the dashes which are present in the returned string if you don't need them.

I need a number (integer or/and long, no decimals). Not a string.

0

In that case just use a random number generator supplied by the standard library. If you want "good quality" random numbers (numbers which can't be predicted), use the SecureRandom class or just use the Random class. Do note that this class doesn't offer strong guarantees like the UUID class.

// E.g.
Random r = SecureRandom.getInstance("SHA1PRNG");
System.out.println(r.nextLong());

BTW, you will get better answers if you actually tell us why you need random/unique numbers...

Edited by ~s.o.s~

0

BTW, you will get better answers if you actually tell us why you need random/unique numbers...

Why? Really no reason; I just want to generate random numbers which are closest to 100% unique.

There honestly is no reason.

0

Actually, there is no such thing in reality as a "perfect" random number generator. There are good ones, and there are ok ones. See this tutorial for more information: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

I like this from the extended summary (the posting is from one of the foremost research labs in the US, Oak Ridge National Laboratory):

Finally, we wax philosophical. We urge the supercomputist to approach the generation of random numbers with circumspection, particularly when solving very large-scale problems. All random number generators should be tested thoroughly for their quality before being used upon other than academic problems. Further, at least two levels of quality in random number generators should be implemented to assess whether the answers are independent of the random numbers. This is a matter requiring judgment and is aided by experience.

Edited by rubberman

0

And as an aside, I have done research into random number generators, and find that I personally like the lagged fibonnaci generators. I used such techniques in the 1980s when developing index fund balancing algorithms for the Mellon Bank / Mellon Capital Management. They worked very well.

3

Actually, there is no such thing in reality as a "perfect" random number generator. There are good ones, and there are ok ones. See this tutorial for more information: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

I like this from the extended summary (the posting is from one of the foremost research labs in the US, Oak Ridge National Laboratory):

And as an aside, I have done research into random number generators, and find that I personally like the lagged fibonnaci generators. I used such techniques in the 1980s when developing index fund balancing algorithms for the Mellon Bank / Mellon Capital Management. They worked very well.

I know that 100% random numbers are impossible but I want to get as close to 100% as possible.

Its out of curiousity nothing else...

0

i think,the simplest way to get random but distinct values is to use shuffle of collection class.

List<Integer> list = new ArrayList<>();
for(int i = min; i <= max; i++) list.add(i);
Collections.shuffle(list);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.