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

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 attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

10 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

C (gcc), 48 43 bytes

s;r(n,m){s+=rand()%m+1;return--n?r(n,m):s;}

Try it online!


Previous 48 bytes version using loop:

i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Python 3, 61 bytes

lambda n,m:sum(randint(1,m)for i in[0]*n)
from random import*

Try it online!

This is not an interesting answer as it is just a copy of your code. I tried to do it with choices but it's 1 byte longer : Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Ruby, 28 bytes

->n,m{(1..n).sum{rand(m)+1}}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Ruby, 27 24 bytes

->n,m{eval'-~rand(m)'*n}

Attempt This Online!

If we change the order of n, m parameters to m, n then following 23 bytes version work, but only in ruby 2.7 (it does not work in 3.x - bug or feature?):

->{eval'-~rand(_1)'*_2}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Dyalog APL, 2 bytes

?⍴

Dyadic 2-train, takes $n$ as its left argument and $m$ as its right argument

The Roll function ? expects an array of maximum bounds and replaces each item with a random number between 1 and that bound (ie ?2 6 5 returns (random between 1 and 2) (random between 1 and 6) (random between 1 and 5). Reshape is used to convert $n$ and $m$ to the list $[m, m, \dots(n\text{ times}), m]$.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python 3, 59 bytes

lambda n,m:sum(choices(range(m),k=n))+n
from random import*

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal ṪR, 3 bytes

(⁰℅

Try it Online!

(   # N times...
  ℅ # Generate a random integer between one and...
 ⁰  # First argument
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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 attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

This works, too: +/>:?$/ n m (2 comments)
+1
−0

Japt -mx, 3 bytes

Takes n as the first input and m as the second.

ÒVö

Try it

ÒVö     :Implicit map of the range [0,first input)
Ò       :Negate the bitwise NOT of (i.e., increment)
 V      :Second input
  ö     :Random int in range [0,V)
        :Implicit output of sum of resulting array
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Jelly, 4 bytes

X}€S

Try it online!

How it works

X}€S - Main link. Takes n on the left, m on the right
  €  - Over each element of 1 through n:
 }   -   With m as its argument:
X    -     Yield a random integer from 1 to m
   S - Sum
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »