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

2018/3/26

getting random number

Miguel.Valdez Miguel.Valdez

2018/3/26

#
so for my object to turn a random direction. Either up, down, left or right. I used
1
turn(Greenfoot.getRandomNumber(4)*90);
wouldnt this mean that the options would be 0, 90, 180, 270, and 360? my professor said "getRandomNumber will return 0, 1, 2, 3, 4, meaning that going forward is 2x as likely. 0.5" I'm just trying to get clarification, THANK YOU
danpost danpost

2018/3/26

#
Miguel.Valdez wrote...
so for my object to turn a random direction. Either up, down, left or right. I used
1
turn(Greenfoot.getRandomNumber(4)*90);
wouldnt this mean that the options would be 0, 90, 180, 270, and 360? my professor said "getRandomNumber will return 0, 1, 2, 3, 4, meaning that going forward is 2x as likely. 0.5" I'm just trying to get clarification, THANK YOU
If you are ever unsure about a method, check the documentation on it. See here.
Miguel.Valdez Miguel.Valdez

2018/3/26

#
my theory is that it will get a number of between 0 - 4 concluding this: 0*90=0 1*90=90 2*90=180 3*90=270 4*90=360 am i correct or wrong on this?
danpost danpost

2018/3/26

#
Did you not take a look at the linked documentation?? -- there is no 4 !!

getRandomNumber

public static int getRandomNumber(int limit) Return a random number between 0 (inclusive) and limit (exclusive). Parameters: limit - An upper limit which the returned random number will be smaller than. Returns: A random number within 0 to (limit-1) range.
Miguel.Valdez Miguel.Valdez

2018/3/26

#
I did but did not take into consideration the exclusive part. So what my professor said was wrong.
You need to login to post a reply.