Page 1 of 1
Random Numbers
Posted: Mon Jan 27, 2014 4:18 pm
by jgordon
We are programming a game, and we have gotten to the point to where we need a ball to start moving at a random angle. How can we create a variable that will set itself to a minimum and maximum value and chose randomly in between?
Re: Random Numbers
Posted: Mon Jan 27, 2014 5:15 pm
by Dixie
have a look at random in the dictionary... especially the note at the bottom
To generate a random number between two integers, use a handler like this:
Code: Select all
function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
Re: Random Numbers
Posted: Mon Jan 27, 2014 7:09 pm
by dunbarx
Hi.
What Dixie said.
And another way, a little easier to understand. If you wanted a number between, say, 100 and 150:
put random(50) + 100
Craig Newman
EDIT: However simple to understand, it does preclude 100. So if a strict, fully inclusive range is required, a little tweaking is needed.