On 06/01/06, Jim Freeze <jim / freeze.org> wrote:
> On Jan 6, 2006, at 12:56 PM, Ruby Quiz wrote:
>> The task for this Ruby Quiz is to write a dice roller. You should
>> write a program that takes two arguments: a dice expression followed
>> by the number of times to roll it (being optional, with a default of
>> 1). So to calculate those stats for your AD&D character, you would do
>> this:
>>
>> roll.rb "3d6" 6
>>	72  64  113  33  78  82
> Ok, I'm still a little confused. This should have output something
> like:
>    rand(16)+3 rand(16)+3 rand(16)+3

Okay, that output is bogus. However, it is not rand(16) at all. It's:

  (1..3).inject(0) { |sum, ii| sum + (rand(6) + 1) }

The fact that it is three 6-sided dice rolled is important (and is
perhaps more important in a PRNG) because the weighting is a little
different. With rand(16) + 3 you're just as likely to get 3 as you are
18. With three rand(6) + 1 values, you're going to get much closer to a
bell curve than a straight probability line. This is a good thing,
because in D&D, 10 is described as absolutely average and 12 is the
high-end for most people. Adventurers, of course, can go to 18, but even
16 is good. Gandalf would be an 18 INT; Sam might be an 11 INT (INT ==
"intelligence").

>> Or, for something more complicated:
>>> roll.rb "(5d5-4)d(16/d4)+3"
>>	31
> What is the -4 and the /d4 do?

  (5d5-4)	=> Roll a 5-sided dice 5 times and take the sum, subtract 4.
			=> Result will be between 1 and 21.
  (16 / d4)	=> Roll a 4-sided dice and divide 16 by the result.
			=> Result will be 4, 5, 8, or 16.
  d			=> Roll a [4, 5, 8, or 16]-sided dice 1-21 times and total.
			=> The total result will be between 1 and 336.
  +3		=> Add three to the result.
			=> The final result will be between 4 and 339.

> Does the +3 apply to (5d5-4)d(16/d4) or to (16/d4) only, assuming it
> matters since I don't know what this stuff does.

  d binds tighter than addition.

>> A few more things...  Feel free to either craft this by hand or an
>> available lexing/parsing library.  Handling whitespace between
>> integers and operators is nice.  Some game systems use d100 quite
>> often, and may abbreviate it as "d%" (but note that '%' is only
>> allowed immediately after a 'd').

> So d100 == d% == d00

Yes.

> and

> 100 == 00

No. d00/d%/d100 all refer to values from 1 to 100. It should be
considered impossible to get a value of 0 from dice. Strictly speaking,
d100 should be a special case simulated where you are rolling two d10
values and treating one of them as the 10s and one of them as the 1s.
Again, it results in a slightly different curve than a pure d100 result
would be. One gaming system developed by Gary Gygax after he was ousted
from TSR in the mid-80s used what he termed d10x, which was d10*d10,
resulting in values from 1 - 100 with a radically different probability
curve than a normal d100.

The "natural" dice created are:

  d4, d6, d8, d10, d12, d20

Novelty dice created in the past include:

  d30, d100

The latter is quite unwieldy.

Strictly speaking, it is not possible to make a die (polyhedron) with an
odd number of faces, but d5 can be simulated by doing a rounded d10/2 or
d20/4.

-austin
--
Austin Ziegler * halostatue / gmail.com
               * Alternate: austin / halostatue.ca