[QUIZ] Dice Roller (#61)

R

Ross Bamford

Time to release your inner nerd.

The task for this Ruby Quiz is to write a dice roller.

Well, I'm no D&Der, but I think I'm gonna hand in my solution for this one
as my first Ruby Quiz entry :)

Cheers,
 
J

Jim Freeze

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
Or, for something more complicated:

> roll.rb "(5d5-4)d(16/d4)+3"
31

What is the -4 and the /d4 do?

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.
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

and

100 == 00

correct?
 
M

Matthew D Moss

The quiz shows incorrect output for 3d6. The line reading "72 64 113
33 78 82" was mistakenly copied from a different set of dice.

"3d6" means: (rand(6)+1) + (rand(6)+1) + (rand(6)+1)

(The +1 are because rand is zero-based, but dice are one-based.)

- is subtraction, so -4 means subtract 4.

/ is division, so /d4 means roll a d4 and divide that into the
expression left of the /

d% == d100

d00 is not valid; there is no such thing as a zero-sided die (although
if you want to make 00 an extension to imply d100 in your own
implementation, that's fine).
 
M

Matthew D Moss

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.

This dice roller is, for the most part, a simple integer calculator
with addition, subtraction, multiplication, division, and grouping via
parentheses. In order to turn it into a dice calculator, we add the
'd' (dice) binary operator. The required right argument to 'd' is the
number of sides on the die, while the option left argument (defaulting
to 1) is how many to roll and sum.

So 16 / d4 means "roll one 4-sided die and divide the result into 16".
 
A

Austin Ziegler

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 =3D=3D
"intelligence").
What is the -4 and the /d4 do?

(5d5-4)=09=3D> Roll a 5-sided dice 5 times and take the sum, subtract 4.
=09=09=09=3D> Result will be between 1 and 21.
(16 / d4)=09=3D> Roll a 4-sided dice and divide 16 by the result.
=09=09=09=3D> Result will be 4, 5, 8, or 16.
d=09=09=09=3D> Roll a [4, 5, 8, or 16]-sided dice 1-21 times and total.
=09=09=09=3D> The total result will be between 1 and 336.
+3=09=09=3D> Add three to the result.
=09=09=09=3D> 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.
So d100 =3D=3D d% =3D=3D d00
Yes.


100 =3D=3D 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
 
R

Robert Retzbach

Ruby said:
The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!
Huhu.
How do you parse 5d6d7?
As (5d6)d7 or 5d(6d7) since there is no "Assoziativgesetz" like (AdB)dC
== Ad(BdC).
-
aTdHvAaNnKcSe
 
M

Matthew Moss

How do you parse 5d6d7?
As (5d6)d7 or 5d(6d7) since there is no "Assoziativgesetz" like (AdB)dC
=3D=3D Ad(BdC).

All binary operators are left associative, so 5d6d7 is (5d6)d7.
 
S

Sascha Abel

Moin,

Austin said:
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.

How exactly would those d10s differ from a d100?

< 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.

Not only a different curve, but also some values would be impossible to
get (as 13 and 51)

*Sascha
 
J

James Edward Gray II

< One gaming system developed by Gary Gygax after he was ousted

Not only a different curve, but also some values would be
impossible to
get (as 13 and 51)

Na, if you get a 1 on the tens die and a 3 on the ones die, you have
rolled a 13.

James Edward Gray II
 
A

Austin Ziegler

How exactly would those d10s differ from a d100?

In the same way that 3d6 is different than rand(16)+3. It's not
necessarily as dramatic a difference, but IME, the incidences of the
very lows (01-19) and very highs (81-00) are not as common as those in
the middle.
Not only a different curve, but also some values would be impossible
to get (as 13 and 51)

Yes.

-austin
 
M

Morus Walter

Or, for something more complicated:

> roll.rb "(5d5-4)d(16/d4)+3"
31

What's the execution order in this case?
Do 5d5-4 rolls with 5d5-4 probably different dices having 16/d4 sides
(number of sides calculated for each roll individually) or should one
choose the number of sides once for all rolls?
I guess it doesn't make much difference but it should be specified...

Morus
 
A

Austin Ziegler

Na, if you get a 1 on the tens die and a 3 on the ones die, you have
rolled a 13.

That's for d%; I was referring to "Cyborg Commando" which had a d10x,
which is (d10)*(d10), making a 1,3 combination 3 always. You'd never get
a prime number larger than 7 under the d10x system.

combo =3D Hash.new(0)

1.upto(10) { |i|
1.upto(10) { |j|
combo[i * j] +=3D 1
}
}

There are 42 possible values here, and 9 values (6, 8, 10, 12, 18, 20,
24, 30, 40) appear four times each. Four values (4, 9, 16, 36) appear
three times each, 23 values twice each, and 6 values once.

It was a truly fucked up system. I think it's because he was mad to be
ousted.

-austin
 
J

Jim Freeze

On Jan 6, 2006, at 4:31 PM, Austin Ziegler wrote:

[great explanation snipped]
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.

If the 10's dice is 3 and the 1's dice is 1, you get 31.

What do you need to roll to get a 0 and 100?

I could see this working if the dice were 0..9 and you add one to the
final result,
but you said that dice should be 1..x. So do you subtract one from each
digit, then add one to the final result?

Example:

10's 1's
1 1 => (1-1)(1-1) => (00)+1 => 1
4 1 => (4-1)(1-1) => (30)+1 => 31
10 10 => (10-1)(10-1) => (99)+1 => 100


Jim
 
J

Jim Freeze

A zero on the tens dice is 10. On the one's dice, it's zero. 00
is 100.
That doesn't jive with what was said earlier. There should be no zero
on the tens dice. Only 1..10.

Jim
 
J

Jacob Fugal

A zero on the tens dice is 10. On the one's dice, it's zero. 00 is
100.

Clarification: presented in short, long and practical. :)

Short clarification:

Actually, when rolled together, both dice are zero-based. The
double-nought is the only special combination of 00 -> 100. When
rolled singly, a d10 has 0 -> 10. Rolling a 0 is never possible.

Long clarification:

Normally, a d% is rolled as a combination of a "d100" and a d10.
"d100" is in quotes, because it's actually just a special d10 -- 10
sided die, that is -- except the numbers on the "d100" are 00, 10,
20... 90. The numbers on the d10 are 0, 1, 2... 9. Rolling the two
together and adding you have a range from 0..99. However, since the
tables that require a d% roll are normally 1-based (1..100), the
'double-nought' -- a 00 on the "d100" and 0 on the d10 -- is
considered 100, everything else is face value. Some examples:

00 / 5 -> 5
10 / 5 -> 15
20 / 0 -> 20
00 / 0 -> 100

Similarly, when asked to roll a d10, the face numbers are 0..9, but
are interpreted as 1..10 by making the 0 a 10 and leaving the other
faces at face value.

All other dice (in my experience) are always interpreted as face value
(the sides being 1-based).

Regarding the probability curve of a d% versus a true d100 (100-sided
die), they are the same. Consider the d100: there are 100 faces, each
with a 1% probability. With a d% roll ("d100" + d10), each integer
between 1 and 100 (again, double-nought counting as 100) is produced
exactly once, and with the same probability. 53 (produced only by 50 +
3) is no more likely than 99 (90 + 9) or 1 (00 + 1). So for all
intents and purposes, a d% is equivalent to a d100.

Practical clarification:

As mentioned above, rolling two ten-sided dice versus rolling a
100-sided dice produce the same distribution (given the method of
combination). Rolling a ten-sided zero-based die then converting 0 to
10 versus rolling a ten-sided one-based die produce the same
distribution. So if you see dM then rand(M) + 1 will produce the
correct distribution. d% counts as d100.

Now if you'll excuse me, I'm late for an appointment with *my* dice.
Your die-rolling lesson for the day was brought to you by the numbers
3, 5 and the letter D. :)

Jacob Fugal
 
H

hitesh.jasani

1+2*3 == (1+2)*3 == 9?

You may want to review the precedence order again.

* All binary operators are left-associative.
* Operator precedence:
( ) highest
d
* /
+ - lowest
 
J

Jim Freeze

Clarification: presented in short, long and practical. :)

Short clarification:

Actually, when rolled together, both dice are zero-based. The
double-nought is the only special combination of 00 -> 100. When
rolled singly, a d10 has 0 -> 10. Rolling a 0 is never possible.

No wonder I don't play D&D. I don't think I am smart enough.

What does 0 -> 10 mean. Does it mean a dice can have the
values 0,1,2,3...10?

If so, why is a 0 never possible?

And why does d10 have 0 -> 10 while a d6 has 1 -> 6?

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top