So I'm trying to generate a random long, preferably in the full range between Long.MIN_VALUE and Long.MAX_VALUE. My first (very naive) attempt was "Long.MIN_VALUE + (long) (Math.Random() * (Long.MAX_VALUE - Long.MIN_VALUE + 1));", obviously this didn't work because the calculation overflowed. I eventually got lazy and decided to just use the positive numbers, so I tried "(long) Math.Random()*(Long.MAX_VALUE);" but for some reason this always returns 0. I'd really like to know why this doesn't work and I'd appreciate any ideas on how to accomplish my goal. I thought about using a BigInteger, but this code needs to be speedy. Thanks for your help.
Agree the java.util.Random class’ method is a better solution, but you can sort out that multiplication and cast if you remember that cast has a higher precedence than * and add one pair of () appropriately.