Fwd: Help with random

Jeanne A. E. DeVoto revolution at jaedworks.com
Sun Feb 4 21:04:08 EST 2007


At 7:24 PM -0500 2/4/2007, Kevin J wrote:
>Ya that I can do. Sorry I should have been a little more clear. I have to
>input fields and a button, input field 1 in the number of dice field 2 is
>the dice type. So when a user enters the numbers ie: 3 die 6, 10 die 6. I
>need to to basically do the same thing but not have to use that many puts
>lol.


Try this:

put field 1 into numberOfDice
put field 2 into numberOfSides
repeat for numberOfDice times
  add random(numberOfSides) to rollTotal
end repeat

numberOfDice and numberOfSides are self-explanatory. (You don't really have to put the fields into variables, but it makes the rest easier to read. Also, reading from a variable is much faster than reading from a field - not likely to be a problem here, but it's a good habit to get into putting field contents into a variable if your routine uses the data several times.)

The repeat loop rolls the die whichever number of times you selected. For each roll, it adds the random number to the total. (You don't need to declare the variable rollTotal - the first time you put something into it, it will be created automatically as an empty variable. Adding a number to "empty" is like adding it to zero.)

Once you've finished the routine, the variable rollTotal contains the total rolled.
-- 
jeanne a. e. devoto ~ revolution at jaedworks.com
http://www.jaedworks.com



More information about the use-livecode mailing list