I came up with this wicked original dice roll function for my RPG awhile back. It returns the total for any number of rolls for any number sided die and even lets you do - or + simply by passing familiar strings to it such as "2d6" or "1d100+25"! The syntax is simple: You pass a string to the function that has the number of rolls and the number of sides to the die separated by a lowercase "d" and optionally you can add or subtract from the total with a "-" or "+" followed by another number.

First you need to declare the function near the beginning of your program

Code: Select all

`DECLARE FUNCTION Roll% (dice$)`

Code: Select all

`RANDOMIZE TIMER`

Code: Select all

```
FUNCTION Roll% (dice$)
totalroll% = 0
FOR rolln = 1 TO VAL(MID$(dice$, 1, INSTR(dice$, "d") - 1))
totalroll% = totalroll% + INT(RND * VAL(MID$(dice$, INSTR(dice$, "d") + 1, LEN(dice$)))) + 1
NEXT rolln
Roll% = totalroll%
IF INSTR(dice$, "+") > 0 THEN Roll% = totalroll% + VAL(MID$(dice$, INSTR(dice$, "+") + 1))
IF INSTR(dice$, "-") > 0 THEN Roll% = totalroll% - VAL(MID$(dice$, INSTR(dice$, "-") + 1))
END FUNCTION
```

Code: Select all

```
damage% = Roll%("4d8+5")
monsters.hp = monsters.hp - damage%
PRINT "You dealt "; damage%; " worth of whoopascii!"
```

anytime you call the Roll% function with a valid value for string "dice$" it returns a total value rolled. For instance to generate a value between 2 and 12 you might roll "2d6". To generate a value between 14 and 50 you might roll "4d10+10". To generate a 1 or a 0 you could simply ask for "1d2-1". The point is that RPG fans already understand the concept of "one die two minus one" and my function makes this easy and fun to work with.

Always remember that the more dice involved the more statistical the probability that you'll roll a value close to the middle of the range. For instance with only one die being cast you have an equal probability across the entire range, but with 2 dice that probability becomes triangle-shaped. With a third die you have have a fat bell curve and with each successive die added the curve gets more and more narrow. So while "1d12", "2d6" and "3d4" may all seem at first glance to generate similar values, it's important to understand that their statistical probabilities are all very different.

All comments and criticisms welcome. Be sure to write back and tell me what you used my code in. Good luck, and enjoy! -coma8