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

2019/10/1

Random objects

vande vande

2019/10/1

#
Hi I want to add a random number of objects between 15 and 20 to the world in random locations
SushiLlama SushiLlama

2019/10/1

#
Hey, first of all you could give us more concrete Information, but ill try my best to help. Well Greenfoot.getRandomNumber(10) gives you a 0,1,2,3,4,5,6,7,8,9. So you would want to add 1 in order to start from 1 and end at 9 -> when you dont want to start from 0, you have to add the offset (difference of the higher number mius the smaller one). In your case that would be 10 because the highest number 25 is 10 away from your small number 15. So your code should look like this: public int getRandomNumber(int startNumber,int endNumber) { int offsetRandom = Greenfoot.getRandomNumber(endNumber-startNumber+1); return offsetRandom+startNumber; }
You need to login to post a reply.