Death By Shock Probe: Das ist eine Menge Würfel


20

In dem Webcomic Darths & Droids behauptet Pete, der R2-D2 in der fiktiven Rollenspielkampagne spielt, auf der der Comic basiert, einmal (Warnung: Potenzielle Spoiler im verknüpften Comic), dass er sich mit dem Lost Orb von Phanastacoria auf seine Seite gestellt hat Schocksonde , er kann jetzt eine satte 1048576d4 austeilendes Schadens. (Der GM hat dies weder bestätigt noch geleugnet.) Da es einigermaßen offensichtlich sein sollte, dass fast niemand die Geduld haben wird, so viele Würfel zu würfeln, schreiben Sie ein Computerprogramm, um dies für ihn zu tun, und geben Sie den Gesamtwert aus, der in einem vernünftigen Rahmen gewürfelt wurde Format. Die Einträge werden nach Programmgröße (kürzestes Programm, Anzahl der Bytes, Anzahl der Siege), sowohl gesamt als auch pro Sprache, sortiert, wobei die Laufzeit unterbrochen wird. Die Antwort kann entweder ein vollständiges Programm oder eine Funktionsdefinition sein.

Scores pro Sprache

Pyth

Maltysen - 8 Bytes *

Jakube - 10 Bytes

APL

Alex A - 10 Bytes

CJam

Optimierer - 11 Bytes

J

ɐɔıɐɔuʇǝɥʇs - 12 Bytes **

Clip10

Ypnypn - 12 Bytes **

K

JohnE - 13 Bytes

Ti-84 BASIC

SuperJedi224 - 17 Bytes *

R

MickyT - 23 Bytes

OCTAVE / MATLAB

Oebele - 24 Bytes

PARI / GP

Charles - 25 Bytes **

Wolfram / Mathematica

LegionMammal978 - 27 Bytes

Perl

Nutki - 29 Bytes

AsciiThenAnsii - 34 Bytes

Rubin

Haegin - 32 Bytes **

ConfusedMr_C - 51 Bytes **

Commodore Basic

Markierung - 37 Bytes **

PHP

Ismael Miguel - 38 Bytes

VBA

Sean Cheshire - 40 Bytes **

Power Shell

Nacht - 41 Bytes **

Javascript

Ralph Marshall - 41 Bytes

edc65 - 54 bytes (Erfordert ES6-Funktionalität, die nicht in allen Browsern verfügbar ist.)

Lua

Kryptychon - 51 Bytes

Java

RobAu - 52 Bytes **

Geobits - 65 Bytes

C

Funktino - 57 Bytes

Python

CarpetPython - 58 Bytes

Postgre / SQL

Andrew - 59 Bytes **

Schnell

Skrundz - 69 Bytes

GoatInTheMachine - 81 Bytes

Haskell

Zeta - 73 Bytes **

ActionScript

Brian - 75 Bytes **

> <>

ConfusedMr_C - 76 Bytes

GEHEN

Kristoffer Sall-Storgaard - 78 Bytes

C #

Brandon - 91 Bytes **

Andrew - 105 Bytes

Ewan - 148 Bytes

Kratzen

SuperJedi224 - 102 Bytes

C ++

Michelfrancis Bustillos - 154 Bytes

Mehrsprachige

Ismael Miguel (Javascript / ActionScript2) - 67 Byte


Top 10 insgesamt

Maltysen
Alex A
Jakube
Optimierer
ɐɔıɐɔuʇǝɥʇs / Ypnypn (Bestellung ungewiss)
JohnE
SuperJedi224
MickyT
Oebele

Mit * gekennzeichnete Warneinträge sind SEHR LANGSAM.

Programmiert markiert ** Ich konnte noch nicht richtig testen


Warten Sie, muss ich die Summe der Würfelwürfe oder nur alle Würfel in einer Liste angeben?
Maltysen

5
Ihre Frage wird in der jetzigen Form wahrscheinlich als unklar oder zu weit gefasst kritisiert. Es wäre sehr hilfreich, wenn Sie in konkreten, objektiven Begriffen beschreiben würden, wie Programme bewertet werden und welche Methoden Programme ihnen zur Verfügung haben sollten. Auch die Notation von 1048576d4kann für einige Benutzer unklar sein. Es wäre hilfreich, genau zu beschreiben, was berechnet werden soll, und alle Richtlinien anzugeben, die befolgt werden müssen.
BrainSteel

2
Dieses Problem kann zu schnell gelöst werden, um ein gutes Zeitfahren zu sein.
Isaacg

12
Sie können versuchen, ein Stack-Snippet-Leaderboard zu erstellen , um zu vermeiden, dass Sie die Liste der Einreichungen manuell auf dem neuesten Stand halten müssen.
Alex A.

1
Ich liebe diesen Titel absolut.
ASCIIThenANSI

Antworten:


10

Pyth - 9 8 Bytes

Verwendet eine offensichtliche einfache Methode zur Summierung von Randint. Es dauerte eine Minute, bis mir klar 1048576wurde 2^20, dass ich mich jetzt wirklich dumm fühle. Vielen Dank an @Jakube, dass Sie mir ein Byte gespart haben, indem Sie darauf hingewiesen haben 2^20 = 4^10.

smhO4^4T

Die Laufzeit ist schrecklich, es ist noch nicht auf meinem Computer zu beenden, so dass kein Punkt ist es online läuft so hier ist die 2^10eine: Probieren Sie es hier online .

s        Summation
 m       Map
  h      Incr (accounts for 0-indexed randint)
   O4    Randint 4
  ^4T    Four raised to ten

4
8 Bytes sind möglich. 2^20 = 4^10
Jakube

@ Jakube danke für den Tipp :)
Maltysen

Dies endet sofort für mich.
Carcigenicate

@Carcigenicate redest du über den Link, den ich gegeben habe? Das ist der Modifizierte, nur die Summe 1024d4.
Maltysen

@Maltysen Ups, sorry. Ja, das ist es.
Carcigenicate

9

Perl - 48 44 37 39 34 Bytes

$-+=rand(4)+1for(1..2**20);print$-

Gibt die Summe ohne nachfolgende Zeile aus.
4 Bytes gespart durch Ersetzen von 2**20(danke Maltysen) und Entfernen von Anführungszeichen um Drucken.
Weitere 7 Bytes durch Umstellen des Codes gespart (Danke Thaylon!)
2 Bytes verloren, da mein alter Code 0-4 generiert hat (es sollte 1-4 sein).
Nochmals 5 Bytes gespart dank Caek und Nutki.

Ungolfed, richtig geschriebener Code:

my $s = 0
$s += int( rand(4) + 1 ) for (1 .. 2**20);
print "$s";

Es war ein wenig schwierig, einen Timer anzuschließen, aber irgendwann funktionierte es.
SuperJedi224

2
Da wir uns nicht um Warnungen kümmern ... $ s + = int rand (5) für (1..2 ** 20); print $ s
Thaylon 30.04.15

3
int(rand(5))
Gibt den

@nutki OK, danke. Ich habe das jetzt bearbeitet.
ASCIIThenANSI

$s+=int rand(4)+1for(1..2**20);print$sDas Entfernen der Klammer für int funktioniert auch für mich, um einen Strich zu speichern.
Caek

7

APL, 11-10 Bytes

+/?4⍴⍨2*20

Dies ergibt nur die Summe eines Arrays von 2 20 = 1048576 Zufallszahlen zwischen 1 und 4.

+/           ⍝ Reduce by summing a
  ?          ⍝ random integer
   4⍴⍨       ⍝ array with values between 1 and 4
      2*20   ⍝ of length 2^20

Sie können dies mit TryAPL vergleichen indem Sie den Zeitstempel davor und danach drucken. Es dauert ungefähr 0,02 Sekunden.

Dank marinus und FUZxxl ein Byte gespart!


Eins und 5 ??? Ein d4 kann 1, 2, 3 oder 4 ergeben. Man kann 5 nicht bekommen.
Loren Pechtel

@LorenPechtel: Entschuldigung, meine schlechte. Vielen Dank für den Hinweis. Es ist jetzt behoben. Ich habe ein müdes Gehirn.
Alex A.

Speichern eines Bytes:+/?4⍴⍨2*20
marinus

Kleine Verbesserung: Verwenden Sie +/?4⍴⍨2*20stattdessen.
FUZxxl

1
Übrigens ist diese Antwort in keiner Weise golfen: Es wäre genau so in der Produktion APL-Code geschrieben.
FUZxxl

7

Ti-84 Basic, 17 Bytes

Gesamter Footprint - Größe des Programmheaders = 17 Byte

Laufzeit: Unbekannt, geschätzt auf 5-6 Stunden basierend auf der Leistung für eine geringere Anzahl von Rollen (also im Grunde nicht sehr gut)

Σ (randInt (1,4), A, 1,2 ^ 20

1
+1 für die Ausführung auf einem TI-84. Ich denke, Zeit ist hier kein Problem, das sind mittlerweile 30-40 Jahre alte Taschenrechner.
ASCIIThenANSI

Ich nehme an, es gibt eine Funktion zum Abtasten einer Normalverteilung und keine einheitliche? Sollte viel schneller sein.
Ben Voigt

@BenVoigt: Da dies das Würfeln simulieren soll, ist eine Normalverteilung nicht angebracht; es müsste einheitlich sein.
Alex A.

2
@AlexA .: Der zentrale Grenzwertsatz sieht vor, dass die Summe vieler gleichförmiger Würfel nicht von einer Normalverteilung zu unterscheiden ist. Es kommt also darauf an, wie pedantisch wir das "Rollen simulieren".
Ben Voigt

1
@MIWright, ich dachte, es sei nur zur Kommunikation gedacht. Zumindest die, die ich habe, verwendet AAA-Batterien.
Arturo Torres Sánchez

7

R 32 24 23 21 Bytes

Bearbeiten: Wurde die as.integerund verwendete Ganzzahldivision los%/% . Beschleunigen Sie es leicht.

Vielen Dank an Alex A für den Probentipp ... und Giuseppe für das Entfernen des r=

sum(sample(4,2^20,T))

Getestet mit

i = s = 0
repeat {
i = i + 1
print(sum(sample(4,2^20,r=T)))
s = s + system.time(sum(sample(4,2^20,r=T)))[3]
if (i == 10) break
}
print (s/10)

Ausgänge

[1] 2621936
[1] 2620047
[1] 2621004
[1] 2621783
[1] 2621149
[1] 2619777
[1] 2620428
[1] 2621840
[1] 2621458
[1] 2620680
elapsed 
   0.029 

Für reine Geschwindigkeit wird das Folgende in Mikrosekunden ausgeführt. Ich bin mir jedoch nicht sicher, ob ich meine Logik richtig verstanden habe. Die Ergebnisse scheinen mit der Zufallsmethode übereinzustimmen. Schade, dass es eine längere Länge ist.

sum(rmultinom(1,2^20,rep(1,4))*1:4)

Hier ist ein Zeitablauf, den ich auf meiner Maschine gemacht habe

system.time(for(i in 1:1000000)sum(rmultinom(1,2^20,rep(1,4))*1:4))
                   user                  system                 elapsed 
7.330000000000040927262 0.000000000000000000000 7.370000000000345607987 

Sie können mit ein paar Bytes speichern sample()anstelle von runif(), dh sum(sample(4,2^20,r=T)).
Alex A.

Habe gerade ein Benchmarking auf meinem Computer durchgeführt und bin auch sample()tatsächlich schneller!
Alex A.

@AlexA. Danke wird testen und ändern, wenn ich in die Nähe eines Computers
komme

Dies oder etwas nicht zu nekrotisieren, aber Sie brauchen es nicht r=T, Tist in Ordnung für den Ersatz.
Giuseppe

1
@Giuseppe, thanks .. this really was an old one
MickyT

6

Python 2, 58 bytes

We get 1048576 random characters from the operating system, take 2 bits of each, and add them up. Using the os library seems to save a few characters over using the random library.

import os
print sum(1+ord(c)%4 for c in os.urandom(1<<20))

This takes about 0.2 seconds on my PC.


6

CJam, 12 11 bytes

YK#_{4mr+}*

This is pretty straight foward:

YK                  e# Y is 2, K is 20
  #                 e# 2 to the power 20
   _                e# Copy this 2 to the power 20. The first one acts as a base value
    {    }*         e# Run this code block 2 to the power 20 times
     4mr            e# Get a random int from 0 to 3. 0 to 3 works because we already have
                    e# 2 to the power 20 as base value for summation.
        +           e# Add it to the current sum (initially 2 to the power 20)

But the beauty of this is that its really fast too! On my machine (and using the Java compiler) it takes on an average of 70 milliseconds.

The online version takes around 1.7 seconds on my machine.

Update: 1 byte saved thanks to DocMax


The online version is taking about 6 seconds from the computers here, but that's probably just the network and/or the macbooks the school insists on using. I'll try again when I get home.
SuperJedi224

@SuperJedi224 The online version is all in JavaScript, does not make any network calls. You can download the Java version and run it using the instructions on the website.
Optimizer

3
Unless I am missing something (which is sadly too common with CJam and me), instead of seeding with 0 and adding 1 for 2^20 runs, seed with 2^20 to save 1 byte: YK#_{4mr+}*
DocMax

@DocMax You are right. Thanks!
Optimizer

+1; I was going to post this exact answer (except with 4A# instead of YK#), but you beat me to it. :)
Ilmari Karonen

6

JavaScript (ES6), 54 bytes

Average time < 100 msec. Run snippet to test (in Firefox)

// This is the answer
f=t=>(i=>{for(t=i;i--;)t+=Math.random()*4|0})(1<<20)|t

// This is the test
test();

function test(){
  var time = ~new Date;
  var tot = f();
  time -= ~new Date;
  
  Out.innerHTML = "Tot: " + tot + " in msec: " + time + "\n" + Out.innerHTML;
}
<button onclick="test()">Repeat test</button><br>
<pre id=Out></pre>

Explanation

With no statistical package built-in, in Javascript the shortest way to obtain the sum of 1 million random number is to call random() for a million times. So simply

f=()=>{
   var t = 0, r, i
   for (i=1<<20; i--; ) 
   {
      r = Math.random()*4 // random number between 0 and 3.9999999
      r = r + 1 // range 1 ... 4.999999
      r = r | 0 // truncate to int, so range 1 ... 4
      t = t+r
   }
   return t
}

Now, adding 1 for a million times is exactly the same than adding 1 million, or even better, start the sum with 1 million and then add the rest:

f=()=>{
   var t, r, i
   for (t = i = 1<<20; i--; ) 
   {
      r = Math.random()*4 // random number between 0 and 3.9999999
      r = r | 0 // truncate to int, so range 0 ... 3
      t = t+r
   }
   return t
}

Then golf, drop the temp variable r and drop the declaration of local variables. t is a parameter, as one is needed to shorten the declaration of f. i is global (bad thing)

f=t=>{
   for(t=i=1<<20;i--;) 
      t+=Math.random()*4|0
   return t
}

Then find a way to avoid 'return' using a nameless inner function. As a side effect, we gain another parameter so no globals used

f=t=>(
  (i=>{ // start inner function body
     for(t=i;i--;)t=t+Math.random()*4|0 // assign t without returning it
   })(1<<20) // value assigned to parameter i
  | t // the inner function returns 'undefined', binary ored with t gives t again
) // and these open/close bracket can be removed too

Doesn't work in chrome. About to test in FF.
SuperJedi224

Of course. Chrome is ES5
edc65

1
It has some ES6 support (most of which is only available by enabling experimental javascript from chrome:\\flags), but does not yet support arrow functions
SuperJedi224

5

Perl, 29

Generates a table of the required length.

print~~map{0..rand 4}1..2**20

I'm getting a syntax error on this one.
SuperJedi224

This needs a new enough version of Perl (the smartmatch operator was introduced in 5.10.1, and I think it wasn't made available by default until later).
Mark

~~ is not a smartmatch, just a double bit inversion to force scalar context. A one character longer way would be print$x=map.... Maybe on newer versions it warns because of ambiguity with smartmatch, but it does seem to work without warnings on my system and in here: ideone.com/LAIWzq
nutki

Yep, it works on IDEone. I'll give it to you.
SuperJedi224

5

J (12 bytes, about 9.8 milliseconds)

+/>:?4$~2^20

I suspect this is mostly memory bandwith-limited: I can't even get it to max out a single core...

You can test this with the following code:

   timeit =: 13 : '(1000 * >./ ($/x) 6!:2"0 1 y)'
   4 20 timeit '+/>:?4$~2^20'
9.90059

This runs it in 4 groups of 20 trails, and returns the number of milliseconds of the avarage time in the quickest group. An interpreter can be found here.


4

Pyth, 10 bytes

u+GhO4^4TZ

This has slightly more bytes than @Maltysen's Pyth solution. But it runs in 8.5 seconds on my laptop, while @Maltysen's solution produced no solution in 20 minutes running time.

But still a little bit too slow for the online compiler.

Explanation

u     ^4TZ   start with G = 0, for H in 0, ... 4^10-1:
                G = 
 +GhO4              G + (rand_int(4) + 1)
             result is printed implicitly 

Will test this this afternoon.
SuperJedi224

4

Java, 65

Since we have scores listed by language, why not throw Java into the mix? There's not much to golf here, just a simple loop, but I was able to squeeze a couple out of my initial attempt:

int f(){int i=1<<20,s=i;while(i-->0)s+=Math.random()*4;return s;}

Will test this this afternoon.
SuperJedi224

No problem. It takes around 80ms on this (slow) PC, but I don't know what you're using to time.
Geobits

I do not believe your program is a correct model. It can and does in my testing add 0 on some rolls. As I understand it most d4's are 1,2,3,4 (no 0 possible).

4
@user39526 s (the total sum) starts at 1<<20 (the number of rolls). This is equivalent to adding one to each roll. When the randomizer throws 0, it's rolled a 1, etc.
Geobits

You should upgrade to Java 8 !codegolf.stackexchange.com/a/52919/7021
RobAu

4

Matlab, 24

First submission ever!

sum(randi([1,4],1,2^20))

I had hoped to make use of randi([1,4],1024), which gives a matrix of 1048576 elements, but then I needed a double sum, which takes more characters than this.

Regarding the running speed mentioned in the question, timeit tells me the runtime is about 0.031 seconds. So, pretty much instant.


I'm getting 0.04 to 0.05 seconds via octave online.
SuperJedi224

4

Haskell, 73 bytes

import System.Random
f=fmap sum.(sequence.replicate(2^20))$randomRIO(1,4)

Usage:

$ ghci sourcefile.hs
ghci> f
2622130

4

C#: 105 bytes

using System.Linq;class C{int D(){var a=new System.Random();return new int[1<<20].Sum(i=>a.Next(1,5));}}

Nice, I like this even if it's two times wrong. It's 1<<20, not 2<<20. And the second parameter of Random.Next is The *exclusive* upper bound of the range so it should be 5
edc65

@edc65 Thanks for catching those errors. I have updated the answer.
Andrew

1
You could save 9 chars by eliminating a and moving the new System.Random() inside of the Sum. Sure, it will create a new Random every time, but who cares as long as it gives a result?
LegionMammal978

@LegionMammal978 if you create a new Random again and again, the result is mostly non-random
edc65

@edc65 That is why I didn't go that route. I haven't had a chance to test what happens if I followed the suggestion.
Andrew

4

PHP, 38 37 bytes

This uses a very simple idea: sum them all!

Also, I've noticed that 1048576 is 10000000000000000000 in binary, equivalent to 1<<20.

Here's the code:

while($i++<1<<20)$v+=rand(1,4);echo$v

Test in your browser (with VERY LITTLE changes):

$i=$v=0;while($i++<1<<20)$v+=rand(1,4);printf($v);

All the changes in the code are explained in comments.


You can remove the ; after echo$v
Martijn

@Martijn I left it there because most of the time PHP complains about it. But I have removed it now. It works on sandbox.onlinephpfunctions.com and that's enough.
Ismael Miguel

4

Mathematica, 30 27 bytes

Tr[RandomInteger[3,2^20]+1]

Mathematica has quite long function names...


3

C, 57 bytes

main(a,b){for(b=a=1<<20;a--;b+=rand()%4);printf("%d",b);}

This code works... once. If you ever need to roll those dice again, you'll need to put srand(time(0)) in there, adding 14 bytes.


Why would you need to add srand(time(0))? (Sorry, I don't use C.)
ASCIIThenANSI

@ASCIIThenANSI Many implementations of C's rand seed it to the same value every run. srand seeds the RNG, and time(0) gets the current time in seconds since 1970.
Functino

If you initialize a=b=1<<20 then you can skip 1+, this saves 4 bytes.
nutki

Also, int before main is not required.
nutki

Hint to anybody doing t=0, then t=t (...) +1 for 1048576 times: think again! (see my answer, eventually)
edc65

3

PostgreSQL, 59 bytes

select sum(ceil(random()*4)) from generate_series(1,1<<20);

I'll admit to the slight problem that random() could, in theory, produce exactly zero, in which case the die roll would be zero.


You don't really need the ; to terminate the query since it is the only one
MickyT

3

Ruby, 32 bytes

(1..2**20).inject{|x|x-~rand(4)}

In a more readable form:

(1..2**20).inject(0) do |x|
  x + rand(4) + 1
end

It creates a range from 1 to 1048576 and then iterates over the block that many times. Each time the block is executed the value from the previous iteration is passed in as x (initially 0, the default for inject). Each iteration it calculates a random number between 0 and 3 (inclusive), adds one so it simulates rolling a d4 and adds that to the total.

On my machine it's pretty fast to run (0.25 real, 0.22 user, 0.02 sys).

If you've got Ruby installed you can run it with ruby -e 'p (1..2**20).inject{|x|x+rand(4)+1}' (the p is necessary to see the output when run in this manner, omit it if you don't care for that or just run it inside IRB where the result is printed to the screen for you). I've tested it on Ruby 2.1.6.

Thanks to histocrat for the bit twiddling hack that replaces x + rand(4) + 1 with x-~rand(4).


1
Could you explain how it works?
ASCIIThenANSI

The first online interpreter I could find that actually wants to load claims that the method rand() doesn't exist. I'll try to find another one.
SuperJedi224

Okay, I found one that works.
SuperJedi224

Bit twiddling hack: x-~rand(4) is equivalent to x+rand(4)+1.
histocrat

Also, you can replace 2**20 with 4e10.
histocrat

3

PARI/GP, 25 bytes

Really, no need for golfing here -- this is the straightforward way of doing the calculation in GP. It runs in 90 milliseconds on my machine. Hoisting the +1 saves about 20 milliseconds.

sum(i=1,2^20,random(4)+1)

Just for fun: if one were optimizing for performance in PARI,

inline long sum32d4(void) {
  long n = rand64();
  // Note: __builtin_popcountll could replace hamming_word if using gcc
  return hamming_word(n) + hamming_word(n & 0xAAAAAAAAAAAAAAAALL);
}

long sum1048576d4(void) {
  long total = 0;
  int i;
  for(i=0; i<32768; i++) total += sum32d4();
  return total;
}

has a very small total operation count -- if xorgens needs ~27 cycles per 64-bit word (can anyone verify this?), then a processor with POPCNT should take only about 0.5 cycle/bit, or a few hundred microseconds for the final number.

This should have close-to-optimal worst-case performance among methods using random numbers of similar or higher quality. It should be possible to greatly increase average speed by combining cases -- maybe a million rolls at a time -- and selecting with (essentially) arithmetic coding.


3

Javascript, 55 53 50 47 41 bytes

for(a=i=1<<20;i--;)a+=(Math.random()*4)|0

I didn't realize that non-random numbers were a known irritant, so I figure that I ought to post a real solution. Meant no disrespect.

Commentary: as noted by others above you can skip the +1 to each roll by starting off with the number of rolls in your answer, and by not having to write a=0,i=1<<20 you save two bytes, and another 2 because you don't add +1 to each roll. The parseInt function does the same thing as Math.floor but is 2 characters shorter.


Note that this answer is completely different from the one originally commented on by SuperJedi224 and @Andrew
Ralph Marshall

You can remove both brackets and the last semicolon (and only the last one) to cut down a few further characters. Also, the current version is only 50 characters, not 52.
SuperJedi224

SuperJedi - thanks for the suggestions. I thought I'd tried it without the brackets only to run into problems, but perhaps I had a different problem. In any case, I think this is about as good as it's going to get.
Ralph Marshall

a+=parseInt(Math.random()*4) may be shortened to a+=1+Math.random()*4&7. The 1+ is only if you care if it rolls 0 or not.
Ismael Miguel

You can golf it down to this: for(a=i=1<<20;i--;)a+=(Math.random()*4)|0, that's only 41 bytes
SuperJedi224

2

Clip 10, 12 bytes

r+`m[)r4}#WT

         #4T    .- 4^10 = 1048576             -.
   m[   }       .- that many...               -.
     )r4        .-          ...random numbers -.
r+`             .- sum                        -.

It takes approximately 0.6 seconds to run on my machine.


2

Go, 78 bytes

Golfed

import."math/rand";func r()(o int){for i:=2<<19;i>=0;i--{o+=Intn(4)+1};return}

Still working on it

Run online here http://play.golang.org/p/pCliUpu9Eq


Unfortunately, the golang.org playground doesn't implement the time operations properly and the repl.it one doesn't want to load right now. I'll see what I can do about it this afternoon.
SuperJedi224


2

Commodore Basic, 37 bytes

1F┌I=1TO2↑20:C=C+INT(R/(1)*4+1):N─:?C

PETSCII substitutions: = SHIFT+E, / = SHIFT+N, = SHIFT+O

Estimated runtime based on runs with lower dice counts: 4.25 hours.

It's tempting to try to golf off two bytes by making C an integer, getting implicit rounding of the random numbers. However, the range on integers in Commodore Basic is -32678 to 32767 -- not enough, when the median answer is 2621440.



2

Ruby, 51 47 chars

x=[];(2**20).times{x<<rand(4)+1};p x.inject(:+)

I looked at all of the answers before I did this, and the sum(2**20 times {randInt(4)}) strategy really stuck out, so I used that.

><>, 76 chars

012a*&>2*&1v
|.!33&^?&:-<
3.v < >-:v >
   vxv1v^<;3
  1234    n+
  >>>> >?!^^

I'm not sure if this one works, because my browser crashed when I tried to test it, but here's the online interpreter.


I'll give you a +1 for the ><> answer.
SuperJedi224

2

Swift, 64 bytes

Nothing clever, golfing in Swift is hard...

func r()->Int{var x=0;for _ in 0..<(2<<19) {x+=Int(arc4random()%4)+1;};return x;}

Version 2 (too late)

var x=0;for _ in 0..<(2<<19){x+=Int(arc4random()%4)+1;};print(x)

2

Java (Java 8) - 52

int f(){return new Random().ints(1<<20,1,5).sum();}
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.