"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
mike mimmis wrote:I am developing a project in which I need to generate unique random numbers between two given input numbers.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Riaan Nel wrote:Hi Mike
Look at the line below.
Math.random() returns a double value between 0 and 1. If you try to cast 0.xxx to an int, you'll always get zero. There isn't really a random number in that line - essentially, it looks just like this:
Also, the very first cast to int looks to be redundant.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:
Riaan Nel wrote:Hi Mike
Look at the line below.
Math.random() returns a double value between 0 and 1. If you try to cast 0.xxx to an int, you'll always get zero. There isn't really a random number in that line - essentially, it looks just like this:
Also, the very first cast to int looks to be redundant.
I think you are mistaken. The calculation will be htnostarts (int) + (diff+1) (int) * (Math.random()) (double). The result of this calculation (int + int * double) will be a double, so the rounding / truncating you mentioned will only take place because of the outer cast to int, which is done over the final result of the calculation, not the intermediate random number.
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jayesh A Lalwani wrote:I think the problem could be that you are using a HashSet. HashSet does not guarantee that the order of the items added in the Set is preserved. I am not sure, but most likely it is reordering your results using the hash code of the results. Since, the hash of an integer is the integer itself, the end result is that your randomly generated numbers are being sorted by the HashSet
Try using a set that preserves order
Dennis Deems wrote:
Jayesh A Lalwani wrote:I think the problem could be that you are using a HashSet. HashSet does not guarantee that the order of the items added in the Set is preserved. I am not sure, but most likely it is reordering your results using the hash code of the results. Since, the hash of an integer is the integer itself, the end result is that your randomly generated numbers are being sorted by the HashSet
Try using a set that preserves order
This. I tried an ArrayList and the output appeared suitably random.
Jayesh A Lalwani wrote:LinkedHashSet will do the trick
Martin Vajsar wrote:
Jayesh A Lalwani wrote:LinkedHashSet will do the trick
Indeed. But shuffling an ArrayList populated with numbers from required range performs better (in O(n) time, compared to O(n^2) of the algorithm in this thread), takes less memory, and probably provides better distribution of possible permutations. Plus, it would make it more clear what the code is meant to do, which should be the most important decision factor of these.
mike mimmis wrote:I am developing a project in which I need to generate unique random numbers between two given input numbers. I have implemented the following but not getting as expected.
Pat Farrell wrote:As written, what you are asking for is mathematically impossible.
If you want a 'pseudo-random' wad of bits that are a function of the two input numbers, simple use a cryptographically strong hash function on the input. Sha256 is part of the standard Java JDK/JRE, and its fairly easy to use.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Pat Farrell wrote:
mike mimmis wrote:I am developing a project in which I need to generate unique random numbers between two given input numbers. I have implemented the following but not getting as expected.
As written, what you are asking for is mathematically impossible. If the result is a function of the two input numbers, its not random.
Mike Simmons wrote:But he didn't say it was a function of the two input numbers - at least, not only those two numbers. He just sait the result must be between those two numbers.
maxUlps the maximum error in terms of Units in the Last Place
or "ulp" stands for "unit of least precision". A difference of one ulp between two floats
indicates that they're "adjacent" floats; that there's no value in between them.
Pat Farrell wrote:
That too is impossible in general.Mike Simmons wrote:But he didn't say it was a function of the two input numbers - at least, not only those two numbers. He just sait the result must be between those two numbers.
While real numbers are infinite, floating points on computers can be adjacent...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
While real numbers are infinite, floating points on computers can be adjacent...
Again. Not really sure what your point is, except that computers can't generate true random numbers. We seem to have managed to write a lot of software that relies on the pseudo-random ones they do produce though. And if you're really that worried about it, you can always make sure that your range limits are separated by at least two ULPs.
Dennis Deems wrote:It's not the numbers themselves that are random, but the selections from among them. Not that the distinction matters terribly.
Paul Clapham wrote:So when your manager requests "random" numbers you don't have to use mathematical definitions of randomness. Even in the unlikely event that your manager is a mathematician, that still isn't what he or she meant. You have a great deal of leeway in practice.
Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.
John von Neumann, 1951, quoted by Knuth
Mike Simmons wrote:However it's also possible to generate true random numbers by observing certain random physical processes (e.g. in quantum mechanics). Most of us don't have such equipment hooked up to our computers, however.
Saurabh Pillai wrote:Is it safe to say that random number generator is actually non-random. It is just that the parameters/inputs used to generate the number is unknown to us. If we know the parameters, we know the next number to be generated?
If we know the parameters, we know the next number to be generated?
Saurabh Pillai wrote:
Mike Simmons wrote:However it's also possible to generate true random numbers by observing certain random physical processes (e.g. in quantum mechanics). Most of us don't have such equipment hooked up to our computers, however.
I do not understand this. Can you give me more information(link or something) explaining from using 'random physical processes' all the way to generated number. I am not able to establish a link here.
Pat Farrell wrote:but it is even true that with a bit of work, you can figure out the parameters from the output.
Paul Clapham wrote:I think that sounds right. When you say you want "unique random" numbers you're already using contradictory terms...
But if after picking a random number from the set you remove it from the set, then you aren't going to get random numbers any more. You are going to get unique numbers, though, which is the more important half of the requirements. And you could certainly say they were randomly chosen.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here