This site requires JavaScript, please enable it in your browser!
Greenfoot back
Codec
Codec wrote ...

2020/9/2

little-crab scenario

Codec Codec

2020/9/2

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

2020/9/2

#
Codec wrote...
Random Numbers. I am a bit confused of how to code "get a random number between -45 and 45".
Minimum value: -45 (starting value). Range: 91 (randomly picked). Hope that helps.
RcCookie RcCookie

2020/9/2

#
If you want to get a random number, you can use
double x = Math.random();
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:
double x = Math.random() * 90;
Here 90 is the max value(exclusive) To get an integer value, just write
int x = (int)(Math.random() * 90);
To get a value between -45 and 45(inclusive), you can do this:
int x = (int)(Math.random() * 91) - 45;
danpost danpost

2020/9/3

#
RcCookie wrote...
To get an integer value, just write
int x = (int)(Math.random() * 90);
To get a value between -45 and 45(inclusive), you can do this:
int x = (int)(Math.random() * 91) - 45;
Greenfoot provides a method to get a random int value back:
int x = Greenfoot.getRandomNumber(91)-45;
This is the standard way to do in greenfoot.
RcCookie RcCookie

2020/9/3

#
Actually didn’t know that
Codec Codec

2020/9/7

#
Thanks guys it worked.
You need to login to post a reply.