After weeks of anticipation, you and your friends get together for your very first game of Dungeons & Dragons (D&D). Since this is the first session of the game, each player has to generate a character to play with. The character's abilities are determined by rolling 6-sided dice, but where are the dice? With a shock, you realize that your friends are waiting for you to produce the dice; after all it was your idea to play D&D! Panicking, you realize you forgot to bring the dice, which would mean no D&D game. As you have some basic coding skills, you quickly come up with a solution: you'll write a program to simulate dice rolls.
For a game of Dungeons & Dragons, each player starts by generating a character they can play with. This character has, among other things, six abilities; strength, dexterity, constitution, intelligence, wisdom and charisma. These six abilities have scores that are determined randomly. You do this by rolling four 6-sided dice and recording the sum of the largest three dice. You do this six times, once for each ability.
Your character's initial hitpoints are 10 + your character's constitution modifier. You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down.
Write a random character generator that follows the above rules.
For example, the six throws of four dice may look like:
Because constitution is 3, the constitution modifier is -4 and the hitpoints are 6.
Most programming languages feature (pseudo-)random generators, but few programming languages are designed to roll dice. One such language is Troll.
In functional languages, most functions are pure. This means that they always return the same output for the same set of arguments, and they do nothing else. In other words, pure functions are deterministic.
However, by definition, a random value is unpredictable and non-deterministic. Even pseudorandomness, that is, returning a sequence of numbers that look random, is not so easy to achieve in a pure context. This is because a pseudorandom number generator (PRNG) needs to keep track of internal state in order to return the next number in the sequence.
Lean offers two possible approaches to this problem:
Use a monad that makes it possible to change the internal state of a PRNG.
There is a readily available function, IO.rand, that does exactly this.
Use pure functions that take a generator as an argument and return not only the pseudorandom value produced, but also an updated generator "primed" for the next number in the sequence.
This exercise uses the second approach. A generator is passed to every function that should produce a pseudorandom value, and the function is expected to return both the value and an updated generator.
Note that a given generator is deterministic, i.e, it always produces the same value. In order to generate the next pseudorandom value in a sequence, it is necessary to use the updated generator.
In this exercise, randomness is checked by a chi-squared test at a significance level of p < 0.0001.
This means that if an ability score is generated according to the instructions, there's less than a 0.01% chance that a correct implementation would fail the test by random chance.
Note that, according to the instructions, an ability score is the sum of the three largest results out of four rolls of an unbiased d6 (six-sided die).
Sign up to Exercism to learn and master Lean with 100 exercises, and real human mentoring, all for free.
We explore how to generate (seemingly) random dice rolls. You'll learn about seeds, pseudo random numbers and some common gotchas. Finally, we'll see how property-based testing is great for testing functions returning random values.