I have a simple question and I’m a little rusty on random number generation. I want to generate large odd integers (I’m using doubles since my numbers could be outside the int range) and I can’t quite figure out how to get rid of the decimals in the random number generation and have the number be odd.
Right now I just have:
N = nMin + (nMax - nMin) * rand.nextDouble();
Which as I said gives me any random number (with decimals) between nMin and nMax. Any help would be much appreciated!
If your numbers can be out of the int range, then you should use
long, or failing in that,BigInteger.Use the information in this question to create a random
BigInteger, and if it is even simply add 1 to it.Alternatively, you could use a method on the BigInteger class:
BigInteger.probablePrime():If it’s probably prime, it’s also probably odd.