Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

81%
+7 −0
Challenges 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...

10 answers  ·  posted 2y ago by Moshi‭  ·  last activity 10mo ago by RubenVerg‭

Question code-golf random
#2: Post edited by user avatar Moshi‭ · 2021-08-26T02:24:54Z (over 2 years ago)
when you realize you forgot to put scoring lol
  • # 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 by user avatar Moshi‭ · 2021-08-26T02:21:18Z (over 2 years ago)
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"