How many dice can you roll without rolling the most probable number

26

0

Problem

Starting from n=2 dice:

  • Roll n dice, with each number 1 to 6 equally likely on each die.
  • Check if their sum equals the most probable sum for n dice, that is 3.5*n.
    • If they're equal, terminate.
    • Otherwise, print n, and repeat from the start with n+2 dice

Your code doesn't have to do this procedure exactly, but should give random output probabilistically equivalent to it, based on our definition of randomness.

Your program should output all of the numbers on their own line; for example, if the program got up to 8 dice and rolled the most probable number with 8 dice, the output would be:

2
4
6

Example Run

On 2 dice, 7 is the most probable sum. Let's say the numbers rolled were 2 and 3. Then, you would print 2.

On 4 dice, 14 is the most probable sum. Let's say the numbers rolled were 3, 4, 2, and 5. Then, the sum is 14, so the program would terminate here.

The final output in this case is "2".

Rules

zoecarver

Posted 2017-06-13T01:53:39.433

Reputation: 635

This answer, as it stands, is very unclear. Is there input, or is it meant to generate the output from no input as a loop? Is there any randomness? I don't seem to see any randomness involved. – HyperNeutrino – 2017-06-13T01:59:25.447

By the way, welcome to PPCG! :) – HyperNeutrino – 2017-06-13T01:59:49.557

Thank you, Sorry I am very new to this. What would make it more clear? There is no input, you are supposed to start with one die and work your way up as high as you can. – zoecarver – 2017-06-13T02:01:40.347

@pudility So if I understand correctly, I am supposed to keep outputting 2, 4, 6, 8, ... an roll that many dice each time until I hit the most probable number for that iteration? – HyperNeutrino – 2017-06-13T02:03:02.683

Yes, that is correct. – zoecarver – 2017-06-13T02:03:43.863

5

Thank you for taking the time to edit your challenge based on our feedback! For the record, we have a place where you can post challenges to work out some of the details before posting: the sandbox.

– FryAmTheEggman – 2017-06-13T02:12:06.927

I think I understand the challenge instructions now, though it took me some time to digest. Would it be OK to simplify things and just say to try even numbers of nice? – xnor – 2017-06-13T02:16:55.683

I think that it would be easier just to leave it how it is now. If you think that it is really important, I can change it. But I think that it makes more sense how it is currently written. Thank you for the suggestion though. – zoecarver – 2017-06-13T02:36:54.690

@pudility I'll give a try at a general cleanup edit, and you see what you think. – xnor – 2017-06-13T02:50:58.773

@pudility I made some edits. Feel free to change things back. I tried to avoid changing any rules, but I do want to suggest allowing a freer output format. In particular, the default is to allow functions as well as programs, which may output (say as a list) rather than print. See this advice on things to avoid.

– xnor – 2017-06-13T03:03:23.027

I added you may use functions as well as programs – zoecarver – 2017-06-13T03:07:51.277

The probability of getting the most probable number with 2n dice is A063419(n)/(6^(2n)).

– Leaky Nun – 2017-06-13T04:07:07.673

I have asked here if some of the current answers that follow a slightly different approach are valid.

– None – 2017-06-13T15:34:30.783

Please do not accept answers when there are shorter answers in too. – Erik the Outgolfer – 2017-06-21T11:43:22.333

Answers

17

Python 2, 70 bytes

from random import*
n=2
while eval("+randrange(6)-2.5"*n):print n;n+=2

Try it online!

The trick is to compute the sum by evaling a string the looks like

'+randrange(6)-2.5+randrange(6)-2.5'

with n copies of the expression concatenated. The randrange(6) outputs a random number from [0,1,2,3,4,5], which is shifted down by 2.5 to have average of 0. When the sum if 0, the while condition fails and the loop terminates.

An alternative using map was 4 bytes longer:

from random import*
n=2
while sum(map(randrange,[6]*n))-2.5*n:print n;n+=2

I've found a bunch of equal-length expressions for a die shifted to mean zero, but none shorter

randrange(6)-2.5
randint(0,5)-2.5
randrange(6)*2-5
uniform(-3,3)//1

xnor

Posted 2017-06-13T01:53:39.433

Reputation: 115 687

11I like this one! Mainly because it is the only one that I understand. – zoecarver – 2017-06-13T03:30:15.430

7

MATL, 13 bytes

`@E6y&Yrs@7*-

Try it online!

Explanation

`       % Do...while top of the stack is truthy
  @E    %   Push 2*k, where k is the iteration index starting at 1
  6     %   Push 6
  y     %   Duplicate 2*k onto the top of the stack
  &Yr   %   Array of 2*k integers distributed uniformly in {1, 2, ..., 6}
  s     %   Sum
  @7*   %   Push 7*k
  -     %   Subtract
        % End (implicit). If the top of the stack is non-zero, the loop
        % proceeds with the next iteration. Else the loop is exited.
        % Display stack (implicit)

Luis Mendo

Posted 2017-06-13T01:53:39.433

Reputation: 87 464

6

Jelly,  19  14 bytes

-5 bytes with help from Leaky Nun (moving from count up to recursion)

‘‘6ṗX_3.L⁶S?Ṅß

A full program printing the results separated by newlines (an extra space and newline are also printed, and the program errors at the end).

Try it online! - any time 6 dice are surpassed TIO kills this due to memory usage, but it works in principle - it also takes ~40s to do so.

A more friendly 15 byte version that does not take so long or require so much memory is available here.

How?

Recursively rolls 2 more dice until the sum of the faces each reduced by 3.5 is zero, printing the number of dice as it goes, when the zero is reached it attempts to use a space character causing a type error.

‘‘6ṗX_3.L⁶S?Ṅß - Main link: no arguments (implicit left=zero)
‘              - increment (initial zero or the previous result)
 ‘             - increment  (= # of dice to roll, n)
  6            - literal 6
   ṗ           - Cartesian power - all possible rolls of n 6-sided dice with faces 1-6
    X          - pick one of them
      3.       - literal 3.5
     _         - subtract 3.5 from each of the roll results
           ?   - if:
          S    -          sum the adjusted roll results (will be 0 for most common)
        L      - ...then: length (number of dice that were rolled)
         ⁶     - ...else: literal ' ' (causes error when incremented in next step)
            Ṅ  - print that plus a newline
             ß - call this link with the same arity (as a monad with the result)

Jonathan Allan

Posted 2017-06-13T01:53:39.433

Reputation: 67 804

Wow, that is very few bytes. Well done! I am holding off on accepting it until a few more people answer. – zoecarver – 2017-06-13T03:10:10.323

Yeah it is normal to wait quite a while before accepting, even if ever doing so. Many people give it a week or two. – Jonathan Allan – 2017-06-13T03:11:05.160

also, you are supposed to output all of the iterations - not just the last one. – zoecarver – 2017-06-13T03:14:09.157

Oh, I answered an old edit - that completely changes it, I cant use this method in many many ways. – Jonathan Allan – 2017-06-13T03:21:04.003

Oh wait just the ns, OK maybe it's salvageable. I thought you meant the sums :) – Jonathan Allan – 2017-06-13T03:22:28.437

Yeah, Many edits where made. Sorry for the confusion. – zoecarver – 2017-06-13T03:23:27.343

18 bytes – Leaky Nun – 2017-06-13T03:44:11.203

Alternative 18 bytes – Leaky Nun – 2017-06-13T03:45:23.493

17 bytes – Leaky Nun – 2017-06-13T03:46:19.887

16 bytes (terminates with error) – Leaky Nun – 2017-06-13T03:47:34.267

15 bytes (trailing newline -> space -> newline, terminates with error) – Leaky Nun – 2017-06-13T03:48:41.137

Note that changing 6ẋX€ to 6ṗX will save a byte, albeit making the program much slower (complexity changes from O(n) to O(6^n)) – Leaky Nun – 2017-06-13T04:11:18.330

@LeakyNun Oh wow, a lot to digest here! I did think there would be savings... – Jonathan Allan – 2017-06-13T04:25:37.413

@JonathanAllan I left the central part of the program unchanged: 6ẋX€_3.S – Leaky Nun – 2017-06-13T04:30:54.913

So you used ‘‘ so that you won't have to insert a space in +2 6ṗ...huh? – Erik the Outgolfer – 2017-06-13T08:45:31.960

@EriktheOutgolfer I think 2+6ṗ... will work just as well. Edit, hmm, nope. – Jonathan Allan – 2017-06-13T08:55:18.660

@JonathanAllan Yeah that's equivalent to 8ṗ. – Erik the Outgolfer – 2017-06-13T08:58:21.330

@EriktheOutgolfer Not exactly - it depends on the rest of the chain how it acts. (e.g. 2+6ẋ on it's own will yield [8,8] not []) – Jonathan Allan – 2017-06-13T09:00:33.403

@JonathanAllan Although 8ṗ2 isn't what you want here. Basically 2+6ṗ is (2)[(+)(6)](ṗ)[2]...you see how badly it progresses, while ‘‘6ṗ is (‘)(‘){(6)(ṗ)}, which just seems optimal, and ‘‘ still adds adds 2 like +2 does, except +26ṗ will progress like [(+)(26)](ṗ)[v] where v doesn't have the correct value by itself, and only adding a space will fix it (+2 6ṗ progresses like [(+)(2)]{(6)(ṗ)}), but nobody is in the mood of adding an extra space if they can so something else. ;) – Erik the Outgolfer – 2017-06-13T09:10:36.413

6

TI-BASIC, 28 bytes

2→N
While mean(randInt(1,6,N)-3.5
Disp N
N+2→N
End

Explanation

  • randInt(1,6,N) generates a list of N random numbers from 1 to 6
  • mean(randInt(1,6,N)-3.5 gets the average of the rolls shifted down by 3.5
  • While continues until the average expression equals zero (the most probable sum)

andrewarchi

Posted 2017-06-13T01:53:39.433

Reputation: 408

5

R, 49 bytes

n=2
while(sum(sample(6,n,T)-3.5)){print(n)
n=n+2}

sample(6,n,T) generates n (pseudo)random samples from the range 1:6 with replacement. Subtracting 3.5 from each element yields a result whose sum is 0 (falsey) if and only if it's the most common value.

Try it online!

Skips the odd dice rolls.

Giuseppe

Posted 2017-06-13T01:53:39.433

Reputation: 21 077

This seems to output 80 every time for me, possible bug? – zoecarver – 2017-06-13T03:13:01.503

@pudility you can add spaces at the end to try it again; it's caching the inputs/code snippet each time – Giuseppe – 2017-06-13T03:14:04.287

3@Giuseppe You can disable the cache in TIO under Settings. – xnor – 2017-06-13T03:20:23.480

after I disabled the cache like @xnor said, It worked very well. Thanks for the answer! – zoecarver – 2017-06-13T03:28:48.357

@xnor who knew! Good to know in the future. – Giuseppe – 2017-06-13T03:49:15.967

4

Java 8, 123 149 113 108 bytes

()->{for(int n=0,s=1,i;s!=n*7;){for(i=s=++n*2;i-->0;s+=Math.random()*6);if(s!=n*7)System.out.println(n*2);}}

Or 107 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-41 bytes thanks to @OliverGrégoire's great thinking!

Explanation:

Try it here.

()->{                           // Method without parameter nor return-type
  for(int n=0,                  //  Amount of dice
          s=1,                  //  Sum
          i;                    //  Index
      s!=n*7;){                 //  Loop (1) as long as the sum doesn't equal `n`*7,
                                //  because we roll the dice per two, and 3.5*2=7
    for(i=s=++n*2;              //   Reset both the index and sum to `n`*2,
                                //   so we can use random 0-5, instead of 1-6
                                //   and we won't have to use `+1` at `Math.random()*6`
        i-->0;                  //   Inner loop (2) over the amount of dice
        s+=Math.random()*6      //    And increase the sum with their random results
    );                          //   End of inner loop (2)
    if(s!=n*7)                  //   If the sum doesn't equal `n`*7
      System.out.println(n*2);  //    Print the amount of dice for this iteration 
  }                             //  End of loop (1)
}                               // End of method

Kevin Cruijssen

Posted 2017-06-13T01:53:39.433

Reputation: 67 575

1I think there is an error in the function. If r equals 3.5*n the program should terminate directly. But, if I understand the function correctly, it would print n one last time before terminating. – raznagul – 2017-06-13T14:48:11.827

@raznagul Actually, it wasn't printing an additional time. It was however bugged. What it did before: random 1-12 (bug 1: should have been 2-12); check if this equals 7: if it is: we're done without printing; if it is not: roll 2 dice again (bug 2, should have been 4 dice instead of 2 again); then print 2, and raise n by 2. So it did contain two bugs (1-12 instead of 2-12; and rolling dice like 2 -> 2 -> 4 -> 6 -> ..., instead of 2 -> 4 -> 6 -> ...). It was printing correctly however, because it wouldn't have gone to System.out.println(n),n+=2 if r was indeed equal to 3.5*n. – Kevin Cruijssen – 2017-06-13T17:07:39.330

2"Roll two dice at once, by picking a random number from 2-12" -- this is not probabilistically equivalent to rolling two dice and adding the numbers as required in the question, therefore is not a correct solution. – Jules – 2017-06-13T18:41:55.303

Just to explain the comment of Jules, when rolling "uniformly" from 2 to 12, you have 1 in 11 chances to get 12. When rolling 2d6, you have 1 in 36 chances to get 12. – Olivier Grégoire – 2017-06-14T08:22:58.670

1Shorter by a few bytes (113), but probably still golfable: ()->{for(int n=2,s=0,e=7,i;s!=e;n+=2,e+=7){for(i=n,s=n;i-->0;)s+=Math.random()*6;if(s!=e)System.out.println(n);}}. Also, correct in regards to Jules' comment and my explanation. n is dices, s is sum, e is expected, i is index. Finally, the sum starts with n to avoid a +1, n times, and s!=e is repeated because I just don't know how to avoid that case. – Olivier Grégoire – 2017-06-14T09:27:26.897

Also, you can use Runnable instead of defining your own interface in the TIO ;) – Olivier Grégoire – 2017-06-14T09:31:14.960

@OlivierGrégoire I indeed know I can use Runnable, but personally I use an interface unless I use currying. Kinda personal preference. As for your code: very nice and smart thinking! I almost feel like I should delete my answer and you should post yours.. Great job! – Kevin Cruijssen – 2017-06-14T10:26:17.747

1I golfed it a bit again ;) ()->{for(int i=0,s=1,j;s!=i*7;){for(j=s=++i*2;j-->0;)s+=Math.random()*6;if(s!=i*7)System.out.println(i*2);}} – Olivier Grégoire – 2017-06-14T17:50:30.933

3

R, 48 44 42 bytes

A 5-byte improvement on Giuseppe's answer.

while(sum(sample(6,F<-F+2,1)-3.5))print(F)

This (ab)uses the fact that F is a variable by default assigned to FALSE which coerces to 0 and can then be incremented, saving us the need to initialize a counter variable.

rturnbull

Posted 2017-06-13T01:53:39.433

Reputation: 3 689

1

of course, you can save two bytes by calling sample(6) instead of sample(1:6) but crossed out 44 is still 44.... https://codegolf.stackexchange.com/a/82343/67312

– Giuseppe – 2017-06-13T13:07:21.220

@Giuseppe Of course, thanks! I've edited the answer now. – rturnbull – 2017-06-14T08:28:41.710

3

05AB1E, 22 20 bytes

-2 Bytes thanks to Emigna

[YF6L.RO}7Y*;ïQ#Y=ÌV

Try it online!

Explanation

[YF6L.RO}7Y*;ïQ#Y=ÌV
[                    # Infinite loop start
 YF     }            # Y times... (Y defaults to 2)
   6L.R               # Push a random number between 1 and 6 (why does this have to be so looooong ._.)
       O              # Sum
         7Y*;ï       # Push 3.5 * Y as an int
              Q      # Is it equal to 3.5 * Y?
               #     # If so: Quit
                Y    # Push Y
                 =   # Print without popping
                  ÌV # Set Y to Y + 2

Datboi

Posted 2017-06-13T01:53:39.433

Reputation: 1 213

1If you move O after .R you can remove ) and s. – Emigna – 2017-06-13T11:17:49.680

2

PHP, 75 bytes

for($d=2;(++$i*7/2-$r+=rand(1,6))||$i<$d;)$i%$d?:$d+=1+print"$d
".$r=$i="";

Try it online!

Jörg Hülsermann

Posted 2017-06-13T01:53:39.433

Reputation: 13 026

15^2/++$i*$d+=rand()%6 is a slightly shorter condition for the loop. Also I think the current loop incorrectly exits if the very first "dice" rolled is a "1" (it generates a 0 for the initial $d). – user59178 – 2017-06-13T13:45:57.190

@user59178 Nice Idea but it could be make a division by zero error so I must modified it. You are right my solution before stops in this case which is wrong. – Jörg Hülsermann – 2017-06-13T14:25:41.753

Your 45-byte answer is invalid because the resulting distribution is not the same as in the question, see here. Your 42-byte answer is, I think, also using the wrong distribution; it seems to assume for example that for two dice, it is equally likely to have 2 and 7 as the sum.

– None – 2017-06-13T18:37:30.463

@Pakk Yes the 45 Byte answer is invalid. I think your thinking is false what happens at the 42 Byte Version. Look at am expanded version Try it online!

– Jörg Hülsermann – 2017-06-13T19:43:39.770

@JörgHülsermann That expanded version confirms what I say. In a proper implementation, the value of $r/$i should become closer to 3.5 for bigger values of $i, but I don't see that happening at all. I got an average of 1.16 for 9984 dice, that is statistically extremely unlikely. – None – 2017-06-13T19:55:57.800

@Pakk I have change my program – Jörg Hülsermann – 2017-06-13T22:25:05.643

1

GolfScript, 41 bytes

0{2+.n\.[{6rand.+5-}*]{+}*!!*.}{}while;;;

Try it online!

Leaky Nun

Posted 2017-06-13T01:53:39.433

Reputation: 45 011

1

JavaScript (ES2015), 75 78 bytes

f=(n=2)=>[...Array(n)].reduce(a=>a+Math.random()*6|0,n)==3.5*n?'':n+`
`+f(n+2)

Outputs a string of results separated by newlines

Edit: saved a byte thanks to Shaggy, added 4 bytes to start function at 2

Explanation

f=n=>
  [...Array(n)]                // Array of size n
    .reduce(                   // Combine each item
      a=>a+Math.random()*6|0,  // Add a random roll between 0 and 5 for each item
    n)                         // Start at n to correct rolls to between 1 and 6
    ==3.5*n                    // Compare total to most probable roll total
  ? ''                         // If true, end
  : n+'\n'+f(n+2)              // Otherwise, output n and continue

f=(n=2)=>[...Array(n)].reduce(a=>a+Math.random()*6|0,n)==3.5*n?'':n+`
`+f(n+2)

let roll = _ => document.getElementById('rolls').innerHTML = f();
document.getElementById('roll-button').onclick = roll;
roll();
<button id="roll-button">Roll</button>
<pre id="rolls"></pre>

andrewarchi

Posted 2017-06-13T01:53:39.433

Reputation: 408

2Save a bytes by using a literal newline enclosed in backticks, instead of '\n'. – Shaggy – 2017-06-13T09:23:25.653

This does not start with n=2, instead you have to specify the starting number of dice when the function is called. – MT0 – 2017-06-13T12:17:47.313

1

Mathematica, 47 bytes

For[n=1,Tr@RandomInteger[5,2n++]!=5n,Print[2n]]

-5 bytes from LLlAMnYP

J42161217

Posted 2017-06-13T01:53:39.433

Reputation: 15 931

1

05AB1E, 17 bytes

[N·ÌD6Lã.R7;-O_#,

Try it online!

Explanation

[                   # loop over N in 0...
 N·Ì                # push N*2+2
    D               # duplicate
     6L             # push range [1 ... 6]
       ã            # cartesian product (combinations of N*2+2 elements in range)
        .R          # pick one at random
          7;-       # subtract 3.5 from each dice roll
             O_#    # if sum == 0 exit loop
                ,   # otherwise print the copy of N*2+2

Emigna

Posted 2017-06-13T01:53:39.433

Reputation: 50 798

1

C (gcc), 84 80 79 77 75 80 78 76 bytes

i;f(s,j){for(;s;s?printf("%d\n",i):0)for(j=i+=2,s=i*7/2;j--;)s-=1+rand()%6;}

Try it online!

cleblanc

Posted 2017-06-13T01:53:39.433

Reputation: 3 360

1

Haskell 133 132 bytes

import System.Random;import Control.Monad
s k=do n<-replicateM k$randomRIO(1,6);if sum n==7*div k 2 then pure()else do print k;s(k+2)

Credit to @Laikoni for the suggestions in the comments below.

Davide Spataro

Posted 2017-06-13T01:53:39.433

Reputation: 211

1.) Imports should be counted in the byte count. 2.) return() can be shortened to pure() and putStrLn$show can be shortened to print. – Laikoni – 2017-06-13T17:48:26.930

I will fix it right away. Thanks – Davide Spataro – 2017-06-13T18:53:31.667

Some further small things: div k 2 then can be div k 2then and do print k;s(k+2) is print k>>s(k+2). – Laikoni – 2017-06-13T20:24:28.480

1

Batch, 109 bytes

@set/an=%1+2,s=n*5/2
@for /l %%i in (1,1,%n%)do @call set/as-=%%random%%%%%%6
@if %s% neq 0 echo %n%&%0 %n%

Rather annoyingly, random is a magic environment variable, so it only gets replaced with a random value during environment expansion, which normally happens before the for loop starts. call makes it happen each time through the loop, but then you need to double the % signs to prevent the expansion from happening before the loop. The fun starts because we want to modulo the result by 6, which requires a real % sign, which now has to be doubled twice. The result is six consecutive %s.

Neil

Posted 2017-06-13T01:53:39.433

Reputation: 95 035

1

php - 89 Characters

$r=0;$n=2;while($r!=$n*3.5){$r=$i=0;while($i<$n){$r+=rand(1,6);$i++;}print $n."
";$n+=2;}

aslum

Posted 2017-06-13T01:53:39.433

Reputation: 251

you need not the first $r=0; use echo instead of print $n." can be write as "$n and for loops instead of while allows do to something in the after loop or before to save some bytes – Jörg Hülsermann – 2017-06-13T17:33:44.683

1

Octave 55 bytes

n=2;
while mean(randi(6,n,1))-3.5!=0
n
n=n+2;
end

Inspired by Andrewarchi's answer. If someone has any pointers to even shorten it, they are welcome.

Michthan

Posted 2017-06-13T01:53:39.433

Reputation: 323

Wow, TI-BASIC and Octave have surprisingly similar syntaxes – andrewarchi – 2017-06-15T01:29:05.783

@andrewarchi Octave (the online version is what I use) is just the basics of the basics when it comes to programming. – Michthan – 2017-06-15T11:11:53.483

1

Pyth, 20 bytes

K2WnsmhO6K*K3.5K=+K2

Try it online!

qwertz

Posted 2017-06-13T01:53:39.433

Reputation: 111

Welcome to PPCG! – Martin Ender – 2017-06-14T12:49:23.060

Thanks! Just finished the Pyth tutorial and figured I might give it a try, although this is probably still improvable. Any suggestions are appreciated. – qwertz – 2017-06-14T13:11:48.873

0

Ruby, 52 bytes

s=x=2;(s=0;p x.times{s+=rand(6)-2.5};x+=2)while s!=0

Explanation

s=x=2;                                                # sum=2, x=2
      (                                  )while s!=0  # while sum != 0:
       s=0;                                           #  reset the sum
           p                                          #  print
             x.times{              };                 #  repeat x times:
                     s+=                              #   Add to sum:
                        rand(6)                       #    random int in 0..5
                               -2.5                   #    subtract average
                                                      #  (implicitly return x for printing)
                                     x+=2             #  Increment x by 2

Try it online!

Value Ink

Posted 2017-06-13T01:53:39.433

Reputation: 10 608

@Pakk note the s=0 at the front of the loop and the use of x.times. This means the sum is reset every time and then x dice are rolled, which should be the correct distribution. I'll write up an explanation of my code. – Value Ink – 2017-06-13T19:23:59.450

You are correct, I was too fast with my conclusion. – None – 2017-06-13T19:51:29.713

0

Calc2 0.7, 119 118 111 bytes

using"runtime";for(n=2,d=0;d!=3.5*n;Console.WriteLine(n),n+=2)for(i=d=0;i++<n;)d+=Math.Int(Random().Next(1,7));

ungolfed:

using "runtime";
var d = 0;
var r = Random();
for(var n = 2; d != 3.5 * n; Console.WriteLine(n), n += 2)
{
    d = 0;
    for(var i = 0; i < n; i++)
        d += Math.Int(r.Next(1,7));
}

I could do without the Math.Int() but unfortunately in 0.7 the Random().Next() functions have a bug where they all return doubles instead of ints. It has been fixed but only after this question was posted. I'm not gonna win anything, but hey, nice proof of concept.

Edit:

  • removed unnecessary space between using and "runtime" (-1 byte)

Edit2:

  • removed var r and create a new Random where it's needed (-4 byte)

  • changed i=0,d=0 to i=d=0 (-2 byte)

  • incremented i after check (-1 byte)

hstde

Posted 2017-06-13T01:53:39.433

Reputation: 159

0

QBIC, 40 bytes

{[1,r|g=g+_r1,6|]~g=r*3.5|_X\g=0?r┘r=r+2

Thispretty much literally does what the challenge asks for; seems the shortest way to get the distribution right.

Explanation

{          DO infinitely
[1,r|      FOR a=1, a<=r (at start, r == 2), a++
g=g+       Add to g (0 at start)
  _r1,6|   a random number between 1 and 6 incl.
]          NEXT
~g=r*3.5   IF the result of all dice rolls equals the expected value
|_X        THEN quit
\g=0       ELSE, reset the dice total
?r┘        PRINT the number of dice used
r=r+2      and add 2 dice.
           END IF and LOOP are courtiously provided by QBIC at EOF.

steenbergh

Posted 2017-06-13T01:53:39.433

Reputation: 7 772

0

Rexx (Regina), 78 bytes

x=2
do forever
  t=0
  do x
    t=t+random(6)
  end
  if(t=x*3.5)then
    exit
  say x
  x=x+2
end

Try it online!

theblitz

Posted 2017-06-13T01:53:39.433

Reputation: 1 201

0

JavaScript (ES6) - 69 Characters

r=n=>n?r(n-1)+(Math.random()*6|0)-2.5:0;f=(n=2)=>r(n)?n+`
`+f(n+2):""

console.log(f())

Explanation:

r=n=>                                     # Roll n dice
     n?                                   # If there is a dice left to roll
       r(n-1)                             #   Roll n-1 dice
             +(Math.random()*6|0)         #   Add a random number from 0 .. 5
                                 -2.5     #   Subtract 2.5 so sum of average is 0
                                     :0   # Else total is 0

and:

f=(n=2)=>                                 # Start with n = 2
         r(n)                             # Roll n dice
             ?n+"\n"+f(n+2)               # If non-zero then concatenate n, newline and
                                          #    result for n+2 dice
                           :""            # If zero (average) terminate.

MT0

Posted 2017-06-13T01:53:39.433

Reputation: 3 373

0

Javascript, 87 chars

for(n=2;eval('+'.repeat(n).replace(/./g,x=>x+(Math.random()*6|0)))!=2.5*n;n+=2)alert(n)

Test with console.log instead of alert:

for(n=2;eval('+'.repeat(n).replace(/./g,x=>x+(Math.random()*6|0)))!=2.5*n;n+=2)console.log(n)
console.log('Done')

Qwertiy

Posted 2017-06-13T01:53:39.433

Reputation: 2 697

0

lua, 102 bytes

function r(n,t) for d=1,n do t=t+math.random(1,6)end return t==n*3.5 or print(n)or r(n+2,0)end r(2,0)

Or the more readable version

function r(n,t) --recursive function does its magic, t is given to safe usage bytes(no local)
    for d=1,n do --roll n dice and count them up to the total (t)
         t =t+math.random(1,6)
    end 
    return t==n*3.5 or --check if t==n*3.5. If it is then it ends
           print(n) or --t != n*3.5 thus print n. print returns nil
           r(n+2,0) --both the check and the return value from print are false thus r gets executed.
end 
r(2,0) --start the process

A more cheaty version for 96 bytes

function r(n,t,m)t=t+m(1,6)+m(1,6)return t==n*3.5 or print(n)or r(n+2,t,m)end r(2,0,math.random)

This pretty much works the same as the first but reuses the rolls from earlier calls. Because of this I can remove the for loop. Both are tested in lua 5.2

lenscas

Posted 2017-06-13T01:53:39.433

Reputation: 31

0

Perl 6, 48 bytes

.say for 2,*+2...^{3.5*$_==sum (1..6).pick xx$_}

Sean

Posted 2017-06-13T01:53:39.433

Reputation: 4 136

-1

PHP, 51 bytes

$r=2;$n=2;while(rand(0,6)-2.5*$r){print $n;$n=$n+2;}

Shiva

Posted 2017-06-13T01:53:39.433

Reputation: 1

If your output is always 2, then this is not a valid answer... – None – 2017-06-13T10:16:02.130

If we print $n inside while loop, then it will print the following : 2,4,6,8,10..... – Shiva – 2017-06-13T10:28:08.820

2Still, I don't see how this follows the requirements in the question. You use two variables: "$n" and "n". "n" is undefined, so will be set to zero. So effectively, what you do is print an even number, and have a chance of 5/6 of printing the next even number. This is mathematically not equivalent to the distribution in the question. – None – 2017-06-13T11:26:29.523

Typo, that n should be always 2, updated the code. – Shiva – 2017-06-13T12:47:10.587

Still not what the question asks... Now you are throwing a die, check if it is five (=2*2.5); if the die is five, you stop, and if it is not five, you write the next even number and continue. Mathematically effectively the same as what you did in the previous version of the code. – None – 2017-06-13T14:35:35.240

I believe $s=0;$n=2;do{$s=$s+7-rand(1,6)-rand(1,6);print $n; n=$n+2;} while($s); (ungolfed) is more in line with what you want, but I am not sure if this is the same distribution... – None – 2017-06-13T15:14:50.663

My suggestion is also wrong, see here.

– None – 2017-06-13T18:34:53.483