21
\$\begingroup\$

In the webcomic Darths & Droids, Pete, who plays R2-D2 in the fictional roleplaying campaign around which the comic is based, once claims (warning: potential spoilers in the linked comic) that, with the Lost Orb of Phanastacoria rigged up to his shock probe, he can now dish out a whopping 1048576d4 of damage. (The GM has neither confirmed nor denied this.) Since it should be reasonably obvious that almost no one will actually have the patience to roll that many dice, write a computer program to do it for him, outputting the total value rolled in some reasonable format. Entries will be ranked by program size (shortest program, by byte count, wins), both overall and per-language, with run time breaking ties. Answer may be either a full program or a function definition.

Scores Per-Language

Pyth

Maltysen - 8 bytes*

Jakube - 10 bytes

APL

Alex A - 10 bytes

CJam

Optimizer - 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

Ruby

Haegin - 32 bytes **

ConfusedMr_C - 51 bytes **

Commodore Basic

Mark - 37 bytes **

PHP

Ismael Miguel - 38 bytes

VBA

Sean Cheshire - 40 bytes **

PowerShell

Nacht - 41 bytes **

Javascript

Ralph Marshall - 41 bytes

edc65 - 54 bytes (Requires ES6 functionality not available in all browsers.)

Lua

cryptych - 51 bytes

Java

RobAu - 52 bytes **

Geobits - 65 bytes

C

Functino - 57 bytes

Python

CarpetPython - 58 bytes

Postgre/SQL

Andrew - 59 bytes **

Swift

Skrundz - 69 bytes

GoatInTheMachine - 81 bytes

Haskell

Zeta - 73 bytes **

ActionScript

Brian - 75 bytes **

><>

ConfusedMr_C - 76 bytes

GO

Kristoffer Sall-Storgaard - 78 bytes

C#

Brandon - 91 bytes **

Andrew - 105 bytes

Ewan - 148 bytes

Scratch

SuperJedi224 - 102 bytes

C++

Michelfrancis Bustillos - 154 bytes

Polyglots

Ismael Miguel (Javascript/ActionScript2) - 67 bytes


Top 10 Overall

Maltysen
Alex A
Jakube
Optimizer
ɐɔıʇǝɥʇuʎs/Ypnypn (order uncertain)
JohnE
SuperJedi224
MickyT
Oebele

Warning- entries marked with a * are VERY SLOW.

Programmed marked ** I have not yet been able to properly test

\$\endgroup\$
13
  • \$\begingroup\$ Wait, do I have to give the sum of the dice roll or just all the rolls in a list? \$\endgroup\$
    – Maltysen
    Apr 30, 2015 at 1:58
  • 6
    \$\begingroup\$ Your question, as it stands, will likely be criticized for being unclear or being overly broad. It would be very helpful if you described in specific, objective terms how programs will be scored and what methods programs should have available to them. Also, the notation of 1048576d4 may be unclear to some users. It would be helpful to provide a description of precisely what should be computed, and any guidelines that must be followed. \$\endgroup\$
    – BrainSteel
    Apr 30, 2015 at 2:05
  • 2
    \$\begingroup\$ This problem can be done too quickly to be a good time trial. \$\endgroup\$
    – isaacg
    Apr 30, 2015 at 2:57
  • 12
    \$\begingroup\$ You could try your hand at making a stack snippet leaderboard to avoid having to manually keep the list of submissions up to date. \$\endgroup\$
    – Alex A.
    Apr 30, 2015 at 14:32
  • 1
    \$\begingroup\$ I absolutely love this title. \$\endgroup\$ Apr 30, 2015 at 22:19

45 Answers 45

1
2
2
\$\begingroup\$

VBA, 40 Bytes

for i=1 to 2^20:q=q+int(rnd*4)+1:next:?q
\$\endgroup\$
8
  • \$\begingroup\$ How can I retrieve the output? \$\endgroup\$ Apr 30, 2015 at 20:57
  • 1
    \$\begingroup\$ @IsmaelMiguel: Paste the line in an immedtiate VBA window. ? is the print statement. \$\endgroup\$
    – edc65
    Apr 30, 2015 at 21:26
  • \$\begingroup\$ I plan on testing this one as soon as I can get visual studio to finish installing \$\endgroup\$ Apr 30, 2015 at 22:13
  • 1
    \$\begingroup\$ @SuperJedi224 Don't need Visual Studio. It's vbA and it's included in Office Applications. Excel for instance \$\endgroup\$
    – edc65
    May 1, 2015 at 19:14
  • 2
    \$\begingroup\$ using excel/word/powerpoint/access, ?timer:for i=1 to 2^20:q=q+int(rnd*4)+1:next:?q:?timer will test it. using a dell latitude E6440, the time was 0.16 secs \$\endgroup\$
    – SeanC
    May 1, 2015 at 21:31
1
\$\begingroup\$

C#, 148 (console app)

using R=System.Random;using C=System.Console;class P{static void Main(){int o=0;R r=new R();for(int i=0;i<1048576;i++){o+=r.Next(1,4);}C.Write(o);}}
\$\endgroup\$
3
  • \$\begingroup\$ You can make your code shorter by removing all unnecessary whitespace, for example, int o = 0; to int o=0;. \$\endgroup\$
    – ProgramFOX
    May 1, 2015 at 9:59
  • \$\begingroup\$ You can also remove the space after C=, and then you'll have 148 chars (at the moment you have 149, not 150). \$\endgroup\$
    – ProgramFOX
    May 1, 2015 at 10:12
  • \$\begingroup\$ It's wrong (just try it: the sum is too low) Should be .Next(1,5), as the second parameter is the exclusive upper bound of the range \$\endgroup\$
    – edc65
    May 1, 2015 at 22:10
1
\$\begingroup\$

Lua 5.1, 51 bytes :)

a=0 for _=1,2^20 do a=a+math.random(4) end print(a)

math.random(n) already returns a number between 1 and n, so I didn't use @edc65's optimization (clever though it is). Another 26 bytes is required to properly seed the RNG, however....

\$\endgroup\$
1
\$\begingroup\$

K, 13 bytes

+/1+(_2^20)?4

Take the sum (+/) of one plus a vector of 2^20 (_2^20) numbers from [0,4) (?4).

The floor _ is necessary because ^ returns a float, which is slightly inconvenient for this problem.

Tested with Kona. On my machine, this runs in roughly 24 milliseconds.

edit:

To test timing, you can prefix the program with \t and get the runtime in milliseconds.

  \t +/1+(_2^20)?4
25
\$\endgroup\$
1
  • \$\begingroup\$ Okay, I'll test this later this afternoon \$\endgroup\$ May 11, 2015 at 16:43
1
\$\begingroup\$

Microscript, 7 bytes

First, a disclaimer: This is not actually a competing entry for this challenge, as this language is significantly too new.

Now, here's the program: 20ec1r4

\$\endgroup\$
1
\$\begingroup\$

Scratch, 102 bytes

The consensus seems to be to convert it to the plaintext form used by the scratchblocks code on the scratch forums, so here we go.

when green flag clicked
set [a v] to [0]
repeat (1048576)
change [a v] by (pick random (1) to (4))
end

Result will be stored in the variable a.

\$\endgroup\$
1
\$\begingroup\$

Swift, 69 bytes

var b={return[UInt32](0..<8<<17).reduce(0){$1*0+$0+arc4random()%4+1}}

$1*0 is required for the compiler to infer the type of the closure.

\$\endgroup\$
1
\$\begingroup\$

Thunno 2 S, 6 bytes

T4@ı4ɼ

Try it online!

Explanation

T4@ı4ɼ  # Implicit input
T4@     # Push 4 ** 10
   ı    # Map over the range:
    4ɼ  #  Random number from [1..4]
        # Implicit output of sum
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 26 22 bytes

{sum (1..4).pick xx 1+<20}

{sum roll 1..4: 1+<20}

It's not fast...takes about nine seconds on my system.

Try it online!

Six years ago, I apparently didn't know about the roll method.

\$\endgroup\$
0
\$\begingroup\$

Actionscript 3, 81 77 75 bytes:

function r(){var a=0,i=1<<20;while(i--){a+=int(Math.random()*4+1)}return a}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ I don't know Actionscript, but if you can write this as a full program rather than a function it will likely be shorter. And are the trailing semicolons on the final lines prior to the closing braces necessary? Also, you can express 1048576 as 2^20, which is much shorter. \$\endgroup\$
    – Alex A.
    Apr 30, 2015 at 19:53
  • \$\begingroup\$ @AlexA. As a full program, you have to write a class declaration in order to have it compile. \$\endgroup\$
    – Brian
    Apr 30, 2015 at 20:10
  • \$\begingroup\$ 1048576 can be replaced with 1<<20. Also, int(Math.random()*4+1) may return 5. Use 1+Math.random()*3>>0 instead. Also, declare it as Actionscript 2 and you have this for you: r=function(){i=a=0;while(i++<1<<20)a+=1+Math.random()*3>>0;return a}. Only 68 bytes!. (or 69, if ; is required after the return) \$\endgroup\$ Apr 30, 2015 at 20:19
  • \$\begingroup\$ @IsmaelMiguel Math.random()*4+1 will never return 5. Math.random() returns 0 <= x < 1. See the Math asdocs for details. \$\endgroup\$
    – Brian
    Apr 30, 2015 at 20:21
  • \$\begingroup\$ Hint to anybody doing t=0, then t=t (...) +1 for 1048576 times: think again! (see my answer, eventually) \$\endgroup\$
    – edc65
    Apr 30, 2015 at 20:38
0
\$\begingroup\$

Javascript + ActionScript2, 67 bytes

According to the ActionScript3 documentation, it is only required to use the keyword var when setting a type.

Other than that, it pretty much looks like Javascript.
Since I'm a fan of polyglots, I did this one.

The code:

function r(){i=a=0;while(i++<1<<20)a+=1+Math.random()*4&7;return a}

The &7 has a function to convert the number to an integer value. I could use other methods, but the gain in size would be null.

Test it here:

function r(){i=a=0;while(i++<1<<20)a+=1+Math.random()*4&7;return a}

document.write(r());

This answer is based on Brian's answer in ActionScript3, based in a comment I left with a solution for him.

\$\endgroup\$
6
  • \$\begingroup\$ Er...you do need var in the variable declaration. Depending on settings, it's a compile or runtime error if you omit it. \$\endgroup\$
    – Brian
    May 3, 2015 at 16:34
  • \$\begingroup\$ @Brian According to the documentation, only if I define a datatype (E.g.: var i:Number;) or "type annotation" (as the documentation calls it). And since I'm providing a value to it, according to the documentation, it won't give me any error. \$\endgroup\$ May 3, 2015 at 16:37
  • \$\begingroup\$ " In ActionScript 3.0, use of the var statement is always required" Are you running as2 or 3? Your answer title says 2, but you cite as 3 documentation. Does your swf run successfully? \$\endgroup\$
    – Brian
    May 3, 2015 at 22:20
  • \$\begingroup\$ @Brian I'm aware of what I'm saying. Quoting: "In ActionScript 2.0, use of the var statement is only required if you use type annotations.". Therefore, the var statement isn't required. I'm reading from ActionScript3 but the code is for ActionScript2. I'm not inventing anything. And I haven't found a way to test it. But I surely will, if I had a way. \$\endgroup\$ May 3, 2015 at 22:36
  • \$\begingroup\$ Ah. Might I suggest adding a note to that effect? Mentioning both as2 and 3 in your answer confuses the issue. \$\endgroup\$
    – Brian
    May 4, 2015 at 14:58
0
\$\begingroup\$

C# - 91

I went the function route for this

Func<int,int>G=s=>{int e=s=0;var r=new Random();for(;s<1<<20;s++)e+=r.Next(1,5);return e;};
\$\endgroup\$
0
\$\begingroup\$

C++, 154 bytes

Golfed:

#include<iostream>
#include <stdlib.h>
#include <time.h>
int main(){int c=0;srand(time(NULL));for(int x=0;x<=1048576;x++){c=c+(rand()%4+1);}std::cout<<c;}

Ungolfed:

#include<iostream>
#include <stdlib.h>
#include <time.h>

int main(){
    int c = 0;
    srand (time(NULL));
    for(int x = 0; x <= 1048576; x++){
        c = c + (rand() % 4 + 1);
    }

    std::cout<<c;
}
\$\endgroup\$
0
\$\begingroup\$

Python 2, 69 67 Bytes

import random
print sum([random.randint(1,4)for _ in range(4**10)])

11 9 bytes more that the existing Python 2 answer :( I'm sure I can find some places to shave off a few bytes though.

\$\endgroup\$
4
  • \$\begingroup\$ I don't think this version gets the distribution right. \$\endgroup\$ Apr 22, 2016 at 18:44
  • \$\begingroup\$ @SuperJedi224 you are 100% correct, this is purely uniform where it should be some flavor of normal. I have an idea that involves sum(), I'll see if I can make it work. \$\endgroup\$ Apr 25, 2016 at 13:14
  • \$\begingroup\$ You can remove the spaces between 4,) and for, and the space between range and (4**10). \$\endgroup\$
    – Riker
    Apr 25, 2016 at 20:58
  • \$\begingroup\$ Good eyes, @EasterlyIrk. Thanks! \$\endgroup\$ Apr 26, 2016 at 13:07
0
\$\begingroup\$

Vyxal s, 38 bitsv2, 4.75 bytes

k₂ƛ4℅

Try it Online!

Explained

k₂ƛ4℅­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁣​‎‏​⁢⁠⁡‌­
k₂ƛ    # ‎⁡For each number in the range [1, 2**20]:
   4℅  # ‎⁢  Choose a random number between 1 and 4
# ‎⁣s flag sums
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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