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
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.
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.
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.
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.
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.
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...
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.
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.
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.
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...
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...
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
Nice but, software/Java only please :)
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);
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.