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
1048576d4
kann 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.Antworten:
Pyth -
98 BytesVerwendet eine offensichtliche einfache Methode zur Summierung von Randint. Es dauerte eine Minute, bis mir klar
1048576
wurde2^20
, dass ich mich jetzt wirklich dumm fühle. Vielen Dank an @Jakube, dass Sie mir ein Byte gespart haben, indem Sie darauf hingewiesen haben2^20 = 4^10
.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^10
eine: Probieren Sie es hier online .quelle
2^20 = 4^10
1024d4
.Perl -
4844373934 BytesGibt 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:
quelle
int(rand(5))
$s+=int rand(4)+1for(1..2**20);print$s
Das Entfernen der Klammer für int funktioniert auch für mich, um einen Strich zu speichern.APL,
11-10BytesDies ergibt nur die Summe eines Arrays von 2 20 = 1048576 Zufallszahlen zwischen 1 und 4.
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!
quelle
+/?4⍴⍨2*20
+/?4⍴⍨2*20
stattdessen.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)
quelle
R
32242321 BytesBearbeiten: Wurde die
as.integer
und verwendete Ganzzahldivision los%/%
. Beschleunigen Sie es leicht.Vielen Dank an Alex A für den Probentipp ... und Giuseppe für das Entfernen des
r=
Getestet mit
Ausgänge
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.
Hier ist ein Zeitablauf, den ich auf meiner Maschine gemacht habe
quelle
sample()
anstelle vonrunif()
, dhsum(sample(4,2^20,r=T))
.sample()
tatsächlich schneller!r=T
,T
ist in Ordnung für den Ersatz.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 therandom
library.This takes about 0.2 seconds on my PC.
quelle
CJam,
1211 bytesThis is pretty straight foward:
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
quelle
YK#_{4mr+}*
4A#
instead ofYK#
), but you beat me to it. :)JavaScript (ES6), 54 bytes
Average time < 100 msec. Run snippet to test (in Firefox)
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
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:
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 off
.i
is global (bad thing)Then find a way to avoid 'return' using a nameless inner function. As a side effect, we gain another parameter so no globals used
quelle
Perl, 29
Generates a table of the required length.
quelle
~~
is not a smartmatch, just a double bit inversion to force scalar context. A one character longer way would beprint$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/LAIWzqJ (12 bytes, about 9.8 milliseconds)
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:
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.
quelle
Pyth, 10 bytes
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
quelle
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:
quelle
s
(the total sum) starts at1<<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.Matlab, 24
First submission ever!
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.quelle
Haskell, 73 bytes
Usage:
quelle
C#: 105 bytes
quelle
The *exclusive* upper bound of the range
so it should be 5a
and moving thenew System.Random()
inside of theSum
. Sure, it will create a newRandom
every time, but who cares as long as it gives a result?PHP,
3837 bytesThis uses a very simple idea: sum them all!
Also, I've noticed that
1048576
is10000000000000000000
in binary, equivalent to1<<20
.Here's the code:
Test in your browser (with VERY LITTLE changes):
Show code snippet
All the changes in the code are explained in comments.
quelle
;
afterecho$v
Mathematica,
3027 bytesMathematica has quite long function names...
quelle
C, 57 bytes
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.quelle
srand(time(0))
? (Sorry, I don't use C.)rand
seed it to the same value every run.srand
seeds the RNG, andtime(0)
gets the current time in seconds since 1970.a=b=1<<20
then you can skip1+
, this saves 4 bytes.int
beforemain
is not required.t=0
, thent=t (...) +1
for 1048576 times: think again! (see my answer, eventually)PostgreSQL, 59 bytes
I'll admit to the slight problem that
random()
could, in theory, produce exactly zero, in which case the die roll would be zero.quelle
;
to terminate the query since it is the only oneRuby, 32 bytes
In a more readable form:
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 forinject
). 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}'
(thep
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
withx-~rand(4)
.quelle
x-~rand(4)
is equivalent tox+rand(4)+1
.2**20
with4e10
.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.Just for fun: if one were optimizing for performance in PARI,
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.
quelle
Javascript,
5553504741 bytesI 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.
quelle
a+=parseInt(Math.random()*4)
may be shortened toa+=1+Math.random()*4&7
. The1+
is only if you care if it rolls 0 or not.for(a=i=1<<20;i--;)a+=(Math.random()*4)|0
, that's only 41 bytesClip 10, 12 bytes
It takes approximately 0.6 seconds to run on my machine.
quelle
Go, 78 bytes
Golfed
Still working on it
Run online here http://play.golang.org/p/pCliUpu9Eq
quelle
Go, 87 bytes
Naive solution
Run online here: http://play.golang.org/p/gwP5Os7_Sq
Due to the way the Go playground works you have to manually change the seed (time is always the same)
quelle
Commodore Basic, 37 bytes
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.quelle
PowerShell,
4137 bytesTook my machine 2 minutes 40 seconds
quelle
Ruby,
5147 charsI 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
I'm not sure if this one works, because my browser crashed when I tried to test it, but here's the online interpreter.
quelle
Swift, 64 bytes
Nothing clever, golfing in Swift is hard...
Version 2 (too late)
quelle
Java (Java 8) - 52
quelle