Random Numbers.
I am a bit confused of how to code "get a random number between -45 and 45".

This site requires JavaScript, please enable it in your browser!

Random Numbers.
I am a bit confused of how to code "get a random number between -45 and 45".

Codec wrote...

Random Numbers.
I am a bit confused of how to code "get a random number between -45 and 45".If you want to get a random number, you can use
This returns a random number between 0(inclusive) and 1(exclusive).
If you want a number in a specific range, you can multiply the number by the maximum value:
Here 90 is the max value(exclusive)
To get an integer value, just write
To get a value between -45 and 45(inclusive), you can do this:

double x = Math.random();

double x = Math.random() * 90;

int x = (int)(Math.random() * 90);

int x = (int)(Math.random() * 91) - 45;

RcCookie wrote...

To get an integer value, just write
int x = (int)(Math.random() * 90);

int x = (int)(Math.random() * 91) - 45;

int x = Greenfoot.getRandomNumber(91)-45;

You need to login to post a reply.