Sam
Sam

Reputation: 137

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.

Upvotes: 1

Views: 167

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533530

integer literals are int type by default. You need to add f for float, d for double and L for long. L is preferred to l as the later can look like 1

e.g.

31 <= 31 as an int
3l <= looks like 31 but is 3 as a long.
31L <= 31 as a long.
311 <= is 311 as an int.

Upvotes: 0

wjans
wjans

Reputation: 10115

Try this

Random generator = new Random(1309233053284l);

You should specify it as a long.

If you call new Random(1309233053284), it will use the constructor taking an int argument. When you call new Random(System.currentTimeMillis()), it's using the constructur taking a long argument since System.currentTimeMillis() returns a long. To make it work, you should also specify 1309233053284 to be a long by adding the l.

Upvotes: 3

aroth
aroth

Reputation: 54816

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).

Upvotes: 10

Related Questions