Means that you want Greenfoot to pick a random number for you between 0 and 19. Greenfoot.getRandomNumber(20) is different from the other methods we called before, it uses the dot notation. Dot notation means the method is not in the class we are using right now, but is in the class Greenfoot. That’s why you need to put the name of the class the method is in, then a dot, then the name of the method we want to use and it’s parameters (if any).Īlright, let’s say we want a ten percent chance that the crab will turn a bit off course every step. So we need to make an expression that will put in place of something–is–true that will return true in exactly 10 percent of the cases. One way to do this is using a random number and a less-than operator. Less than works by comparing two numbers and returning true if the first number is smaller (less than) the second. It uses the < symbol.ģ.1 Try This: On your word document try to figure out an expression using getRandomNumber and < that will when you run it be true exactly ten percent of the time.ģ.2 Try this: Type up another expression that is true 7 percent of the time. An expression that will be true 10 percent of the time could be: When you want to talk about the chance in percent, dealing with random numbers out a hundred is the easiest way to do it. This call will give us a new random number between 0 and 99 every time it is called, so it will be under 10 in ten percent of the times it comes up. This is how you can use it to make the crab turn a little about 10 percent of the time.ģ.3 Try This: Try out the random direct changes shown above in your own version. Tell me what % chance each one would be for turning.) Try out several possibilities for turning. The Code is going good, but we can still do better. It is always changing the same amount (5 degrees) and only turning right, never turning left. What would be better is if the crab turned a small but completely random amount to either its left or its right.ģ.4 Try this: Do you know enough to do this on your own? Write the code and screenshot it here, before reading on to see how I do it. No Cheating Yo! Did you really try it out on your own?ģ.5 Try This: Test out the code you see above. You need to fix an error, but then it will run perfectly. When it compiles successfully what do you see? Does the crab always turn the same amount or does it turn different amounts?ģ.6 Try this: Our stubborn crab is still only turning right. That makes this crab kind of strange, so fix it. If ( Greenfoot.getRandomNumber (100) < 10 ) #Greenfoot random number code#Ĭhange you code so the crab turns either left or right by up to 45 degrees when it turns. Turn( Greenfoot.getRandomNumber (90) -45 ) ģ.8 Try this: What would happen if you dropped a bunch of crabs in your world and ran it. Would they turn at the same time and the same amount? Guess first, based on your thinking and then try it out. Were you right? Explain why the crabs did what they did. The crab is having fun skittering around, but all that exercise is probably making it hungry.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |