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

Comments on Roll n fair dice

Parent

Roll n fair dice

+7
−0

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:

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!

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+1
−0

J, 10 7 bytes

+/>:?#/

Try it online!

+/>:?#/
     #/ : Inserts dyadic # into an array n m
          Creates n copies of m
    ?   : Roll from 0..y
  >:    : Increment
+/      : Sum reduce

-3 bytes thanks to torres.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

This works, too: +/>:?$/ n m (2 comments)
This works, too: +/>:?$/ n m
torres‭ wrote almost 2 years ago

This works, too: +/>:?$/ n m

south‭ wrote almost 2 years ago

Thanks for that, a non-tacit solution is smarter.