3
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ 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? \$\endgroup\$ Jul 4, 2015 at 0:20
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Jul 4, 2015 at 0:25
  • \$\begingroup\$ Rather than no output, can the output be something benign when faces <= throws, like 0 which is impossible in this case? \$\endgroup\$
    – Alex A.
    Jul 4, 2015 at 0:47
  • 4
    \$\begingroup\$ 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? \$\endgroup\$
    – Doorknob
    Jul 4, 2015 at 2:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – lirtosiast
    Jul 4, 2015 at 3:58

4 Answers 4

2
\$\begingroup\$

TI-BASIC, 16 20 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
\$\endgroup\$
0
2
\$\begingroup\$

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...
  ;       discard the number
  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
;       discard the extra [#faces]
]_+*    wrap stack in an array, sum
x/      divide by [#rolls]
\$\endgroup\$
8
  • \$\begingroup\$ These don't look like they have empty input if faces≤throws. \$\endgroup\$
    – lirtosiast
    Jul 4, 2015 at 1:01
  • \$\begingroup\$ Are you sure this isn't Golfscript? \$\endgroup\$
    – Beta Decay
    Jul 4, 2015 at 20:04
  • \$\begingroup\$ @BetaDecay Ostrich was designed to be as similar as possible to GolfScript. ;) \$\endgroup\$
    – Doorknob
    Jul 4, 2015 at 20:24
  • 1
    \$\begingroup\$ @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 \$\endgroup\$
    – Doorknob
    Jul 4, 2015 at 21:37
  • 2
    \$\begingroup\$ @Doorknob Your definition for B is hilarious 😂😂😂 \$\endgroup\$
    – Beta Decay
    Jul 4, 2015 at 21:42
1
\$\begingroup\$

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.").

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't have empty output when faces≤throws. \$\endgroup\$
    – lirtosiast
    Jul 4, 2015 at 0:39
1
\$\begingroup\$

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

Try it here online.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.