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

Dashboard
Notifications
Mark all as read
Challenges

Roll n fair dice

+4
−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!

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

5 answers

+3
−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!

Why does this post require moderator attention?
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
Why does this post require moderator attention?
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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Ruby, 32 bytes

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

Try it online!

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

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!