Random Numbers

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jgordon
Posts: 4
Joined: Mon Jan 13, 2014 4:18 pm

Random Numbers

Post by jgordon » Mon Jan 27, 2014 4:18 pm

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?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Random Numbers

Post by Dixie » Mon Jan 27, 2014 5:15 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10341
Joined: Wed May 06, 2009 2:28 pm

Re: Random Numbers

Post by dunbarx » Mon Jan 27, 2014 7:09 pm

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.

Post Reply