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

2020/11/20

Random Number question

greenanarchist greenanarchist

2020/11/20

#
Hello, I'd like to know what expression should be used for Greenfoot.getRandomNumber to produce even numbers in the range -20 - 20. Can anyone help?
danpost danpost

2020/11/20

#
There are 21 even numbers within that range and the range starts at -20, increasing by 2:
int rand = Greenfoot.getRandomNumber(21)*2-20;
greenanarchist greenanarchist

4 days ago

#
danpost wrote...
There are 21 even numbers within that range and the range starts at -20, increasing by 2:
int rand = Greenfoot.getRandomNumber(21)*2-20;
Great, thank you so much :)
You need to login to post a reply.