# Dice adventure in the river Styx [duplicate]

You're sitting in the school cafeteria, microwaving your food like you always do around lunch. Suddenly the programming devil rises from the ashes of your burnt food and proposes a challenge:

Sup guys, I have a challenge.

How did this happen? What did you ever do wrong? Was it that one bad macro in C?

The programming devil demands a game of dice:

I demand a game of dice.

Strictly speaking, it is not a game of dice. The programming devil just wants you to calculate the mean value of throwing a die n times. The programming devil's twist is that the die can, and always will, have a number of faces larger than n.

## Challenge:

Write a program which calculates and outputs the mean value of n throws with a die with a number of faces greater than n.

## Input:

Two positive integers. The first argument determines the number of throws, the second argument the number of faces of the die. There is no empty input - always two integers.

## Output:

The mean value of throwing the dice n times rounded to the closest integer.

## Requirements:

• No output if the number of faces of the die is less than or equal to the number of throws.
• The values returned from throwing the die has to be random (as close as we can get to random, that is)

The programming devil thinks this is great and will restore the burnt food of the programmer which writes the shortest code in bytes to solve his challenge:

This is a great challenge. I will restore your food if you propose the shortest program in bytes to me.

• Doorknob's answer brings up a good question: Are we actually supposed to generate a random number n times and the provide the mean, or provide the theoretical mean of rolling the dice? Jul 4, 2015 at 0:20
• You're supposed to generate n die throws of a die with a number of faces greater than n and calculate the mean value of all throws. Do you think I can clarify this better in the question somehow? Jul 4, 2015 at 0:25
• Rather than no output, can the output be something benign when faces <= throws, like 0 which is impossible in this case? Jul 4, 2015 at 0:47
• You say that "the die can, and always will, have a number of faces larger than n" but then later "No output if the number of faces of the die is less than or equal to the number of throws." Which one is it? Jul 4, 2015 at 2:59
• Does an error message count as "no output"? In TI-BASIC, error messages are displayed on top of the homescreen, so no characters are written to the homescreen. Jul 4, 2015 at 3:58

# TI-BASIC, 1620 19 bytes

Straightforward. The +8/-1 consensus on meta is that mixing inputs is allowed—so Ans, from command-line input, is the number of faces, and N is the number of throws.

Input N
If N<Ans
int(mean(1.5+int(Ansrand(N


# Ostrich 0.7.0, 18 23 chars

Two solutions, identical in length:

:x;.,{;xR*C}%_+*\/o2/+F
\:x{.R*C\}*;]_+*x/o2/+F


Edit: +5 characters to each because I forgot to round to the nearest integer.

They both actually simulate rolling the dice (and thus have non-constant output), which seems to be the intent of the challenge.

Explanation of #1:

:x;     store the number of faces in x, pop from stack
.,      create range from 0 to [number of rolls] (while keeping #rolls on stack)
{...}%  map over that range...
xR*     generate a float between 0 and [number of faces]
C       take ceil, which will make this an int between 1 and [#faces]
_+*     reduce by {+} (_ is the syntax for a single-instruction block) a.k.a. sum
\/      swap top two stack elements (bringing the number of rolls to the top),
then divide, resulting in the average


Explanation of #2:

\       bring the number of rolls to the top
:x      store number of rolls in x, but don't pop from the stack
{...}*  that many times...
.       duplicate the number of faces
R*C     get the random number as described in #1
\       stick that under the number of faces again, so [#faces] is still on top
]_+*    wrap stack in an array, sum
x/      divide by [#rolls]

• These don't look like they have empty input if faces≤throws. Jul 4, 2015 at 1:01
• Are you sure this isn't Golfscript? Jul 4, 2015 at 20:04
• @BetaDecay Ostrich was designed to be as similar as possible to GolfScript. ;) Jul 4, 2015 at 20:24
• @BetaDecay Not much. The main reason I made it is because Golfscript annoyingly doesn't have a REPL and is missing a few key operators. (see another chat link) Of course, CJam, Pyth, & co. are much better; I haven't gotten around to learning those though :P Jul 4, 2015 at 21:37
• @Doorknob Your definition for B is hilarious 😂😂😂 Jul 4, 2015 at 21:42

# APL, 13 bytes

{0⍕⍺÷⍨+/?⍵⍴⍺}


This creates an unnamed dyadic function where the left argument ⍺ is the number of throws and the right argument ⍵ is the number of die faces. Here we sum (+/) ⍺ random numbers between 1 and ⍵ (?⍵⍴⍺) and divide that by ⍺ (⍺÷⍨). The result is rounded to the nearest integer (0⍕).

You can try it online.

Note that this still produces output even when the number of throws is greater than the number of faces. But per the challenge rules, that does not constitute valid input ("The die can, and always will, have a number of faces larger than n.").

• This doesn't have empty output when faces≤throws. Jul 4, 2015 at 0:39

# Pyth - 17 bytes

I'm sure it can be shorter, but the checking is stopping me from putting it in a lambda.

I>QKvz.RcsmhOQKKZ


Try it here online.

## Or if the second interpretation of the rules is correct, 12 bytes.

Same thing but in a lambda.

M.RcsmhOHGGZ