PDA

View Full Version : Difficulty with random number generation


JasonMichael
04-27-2014, 07:11 PM
Hey folks! I just finished Assignment 4 with the boomboom method. I could not figure out how to create a method called distance that randomly generated numbers in feet, instead of meters. Since it was only meters that I could get to work, I used randomization between 1 and 2 so the character would not go off screen during his jumps.

My question is, how do we set it to feet instead of meters, or can we randomize non-integer meters like randomize from .5 to 2.3 meters with the random numbers rounded off to the nearest tenth? Either one of these solutions would allow for a more varied jump distance while remaining on the screen.

xCROv
04-28-2014, 02:37 AM
Well. A meter is 3 feet; you could do a simple math problem if you wanted it in feet. I have not read that assignment so I'm not sure on the specifics for it but you can make an object walk as far or as short as you want.

jrrTolkien
04-28-2014, 09:33 AM
1 meter is about equal to 3.28 feet. Create a variable, then do set value to a random integer (found in the world.functions tab,) tell it to instantly multiply it by 3.28 (or just 3.3 if you're lazy ;)), and you'll have it in feet. If you want to see it whenever it changes, you can right-click your variable, and select "watch this variable" and it will show up when you select the play button. If you want to avoid all this extra work, though, you can simply just work in meters.

-jrr

chickentree
04-28-2014, 10:05 AM
Hey folks! I just finished Assignment 4 with the boomboom method. I could not figure out how to create a method called distance that randomly generated numbers in feet, instead of meters. Since it was only meters that I could get to work, I used randomization between 1 and 2 so the character would not go off screen during his jumps.

My question is, how do we set it to feet instead of meters, or can we randomize non-integer meters like randomize from .5 to 2.3 meters with the random numbers rounded off to the nearest tenth? Either one of these solutions would allow for a more varied jump distance while remaining on the screen.

First, there is no way I know of to change Alice to use feet automagically. The world, functions and methods are all predicated on the metric system.
Converting value given in feet to meters is however easy.

Create a function called feet2meters that returns a number
Add a numeric parameter called lenInFeet
Add a variable called conversionFactor
Set conversion factor to 0.3048
return lenInFeet * conversionFactor


If you click the on the more ... in the random number function you will find that you can set 3 values a minimum, a maximum, and integerOnly. The min and max can be set to any value as long as min<max and as long as the integerOnly is not set or false, the function will return values between min and max. Note that due to the way it works random number can return the min value but will never return the max value.

I am not sure why you want to round to 10ths, I usually do this for value I want to display as 3d text. To accomplish this write another function that takes in a number and returns the desired value. For tenths:

multiply the given number by 10
round the number using the round function. This drops the decimal part.
divide by 10 to change to tenths.


As an exercise you could try and make this function return any number of decimal places. (hint: Add a parameter for the number of places and use the
a raised to the b power function