### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

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

+4
−0

# Python 3, 61 bytes

lambda n,m:sum(randint(1,m)for i in*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?

+2
−0

# Ruby, 28 bytes

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


Try it online!

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

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

+1
−0

# Python 3, 59 bytes

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


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

# 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;}

Why does this post require moderator attention?

+0
−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}

Why does this post require moderator attention?

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

Why does this post require moderator attention? 