Challenges

+5
−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?
Why should this post be closed?

+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?

+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?

+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?

+1
−0

# Ruby, 32 bytes

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


Try it online!

Why does this post require moderator attention?

+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?

+0
−0

# Ruby, 27 bytes

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


Try it online!

Why does this post require moderator attention?

Featured
Hot Posts
Challenges — "Hello, World!"
Challenges — Coat of Many Colours
Challenges — Cumulative Counts

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

Want to advertise this community? Use our templates!

Like what we're doing? Support us!