Search code examples
javarandomnumberslong-integermilliseconds

Number is too long to be passed to Random() ?


I'm trying to do

Random generator = new Random(1309233053284);

Random being java.util.Random

It says the number is too long, but why can System.currentTimeMillis() be passed to the constructor? It returns even bigger numbers.

1309233053284 are milliseconds, if you're wondering.


Solution

  • You may have better luck with:

    Random generator = new Random(1309233053284L);
    

    In Java, all literal numbers are of type int unless otherwise specified. To get your number interpreted as a long, you need to suffix it with 'L' (or alternately 'l', but that is difficult to distinguish from a '1', and therefore somewhat less clear).