Post History
Challenge This is a simple randomness challenge: Given a non-negative integer $n$, and positive integer $m$, simulate rolling and summing the results of $n$ fair dice, each of which have $m$ sides...
#2: Post edited
- # Challenge
- This is a simple randomness challenge: Given a non-negative integer $n$, and positive integer $m$, simulate rolling and summing the results of $n$ fair dice, each of which have $m$ sides numbered $1$ through $m$.
- Here is a basic ungolfed example in Python:
- ```python
- from random import randint
- def roll(n, m):
- return sum([randint(1, m) for i in range(n)])
- ```
- [Try it online!] (Also includes a basic visualization of the resulting distribution)
- [Try it online!]: https://tio.run/##XY9Bb8IwDIXv@RXvlmSrJsrENIE48TMQB9SmWxBxKseV2K9nbunQ1Fwcy8/fe@5/5DvT@/3ecU7gM7VaYuozy9RFEtOGDpyvV0cVkt8a6OMgAxPKkNxx1rl6HKPLjIhI4/pXcORP/gFvFBEaiZnKn8MhDySBDWGPz51JWj52pshZin7nqRo8zZf0zWqlfGOWphUIL0h4RT0H7nmMWIRd9G98GYq4dQUL61Vkt7ZCoHZvrf@ntjerlCnOMeoZvw "Python 3 – Try It Online"
- # Challenge
- This is a simple randomness challenge: Given a non-negative integer $n$, and positive integer $m$, simulate rolling and summing the results of $n$ fair dice, each of which have $m$ sides numbered $1$ through $m$.
- Here is a basic ungolfed example in Python:
- ```python
- from random import randint
- def roll(n, m):
- return sum([randint(1, m) for i in range(n)])
- ```
- [Try it online!] (Also includes a basic visualization of the resulting distribution)
- This is code golf, so shortest code wins!
- [Try it online!]: https://tio.run/##XY9Bb8IwDIXv@RXvlmSrJsrENIE48TMQB9SmWxBxKseV2K9nbunQ1Fwcy8/fe@5/5DvT@/3ecU7gM7VaYuozy9RFEtOGDpyvV0cVkt8a6OMgAxPKkNxx1rl6HKPLjIhI4/pXcORP/gFvFBEaiZnKn8MhDySBDWGPz51JWj52pshZin7nqRo8zZf0zWqlfGOWphUIL0h4RT0H7nmMWIRd9G98GYq4dQUL61Vkt7ZCoHZvrf@ntjerlCnOMeoZvw "Python 3 – Try It Online"
#1: Initial revision
Roll n fair dice
# Challenge This is a simple randomness challenge: Given a non-negative integer $n$, and positive integer $m$, simulate rolling and summing the results of $n$ fair dice, each of which have $m$ sides numbered $1$ through $m$. Here is a basic ungolfed example in Python: ```python from random import randint def roll(n, m): return sum([randint(1, m) for i in range(n)]) ``` [Try it online!] (Also includes a basic visualization of the resulting distribution) [Try it online!]: https://tio.run/##XY9Bb8IwDIXv@RXvlmSrJsrENIE48TMQB9SmWxBxKseV2K9nbunQ1Fwcy8/fe@5/5DvT@/3ecU7gM7VaYuozy9RFEtOGDpyvV0cVkt8a6OMgAxPKkNxx1rl6HKPLjIhI4/pXcORP/gFvFBEaiZnKn8MhDySBDWGPz51JWj52pshZin7nqRo8zZf0zWqlfGOWphUIL0h4RT0H7nmMWIRd9G98GYq4dQUL61Vkt7ZCoHZvrf@ntjerlCnOMeoZvw "Python 3 – Try It Online"