possible lossy conversion from long to int?

That’s because a long is 64 bits and an int is 32 bits, not to mention you’re going from floating-point to integer. To go from long to int, you’re going to have to discard some information, and the compiler can’t/won’t do that automatically. You’re going to have to explicitly say so through a cast:

int g = (int) Math.round(Math.random() * 255);
        ^^^^^

Alternatively, you can use java.util.Random:

Random r = new Random();
int g = r.nextInt(256);

Leave a Comment