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

2012/9/5

getRandomNumber

Moritz Moritz

2012/9/5

#
Hey, short question: Greenfoot.getRandomNumber(50) -> random number between 0 and 50. but how can I 'create' a random number between 20 and 30? so the minimum hast to be 20 and the maximum 30. thank you =))
MatheMagician MatheMagician

2012/9/5

#
create this method in your code:
public int getRandomNumber(int start,int end)
{
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
}
and call it like this:
getRandomNumber(20,30);
Gevater_Tod4711 Gevater_Tod4711

2012/9/5

#
@MatheMagican good way to solve that. I always just write Greenfoot.getRandomNumber(number)+ohterNumber but this way is better. But Moritz, you should be carefull with this: Greenfoot.getRandomNumber(50) is a random number from 0 to 49. It'll never be 50. This could be very dificult to find if your programm doesn't work bekause of that.
Moritz Moritz

2012/9/5

#
thank you - it works =))
Moritz Moritz

2012/9/5

#
@ Gevater_Tod .. yes I know that -- with 'between' I meant a random number from 0 to 49 =)) thank you anyway =)
MatheMagician MatheMagician

2012/9/5

#
Moritz, something I didn't mention is that my method could return 20 and it could return 30.
You need to login to post a reply.