Stack OverflowXKCD - Random Number
[+115] [14] Preets
[2009-01-20 17:16:55]
[ random fun dice xkcd ]

Could someone explain if and why the following random number function from XKCD [1] works?

I have never quite been able to understand it. I ask the question on SO hoping to find a more "human" explanation that could be understood by me.

RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.

(49) In an infinite sequence of random numbers, you will see infinite sequences of the same number. - Lasse V. Karlsen
If you rewrote the question you may get people voting to re-open it, but as it is, it looks like a bad question. - Kent Fredric
(2) I do not believe a lot of people actually undertand how this work. This question definitely needs to be answered ! - Preets
The dice roll was random, therefore 4 is random. QED. Of course, 4 is not random - that's the joke. - Greg Hurlman
(3) 4 Is random, that's half the problem :p - Kent Fredric
What was wrong with to original question? ... - OscarRyz
I'd say it's the Haskell version of getRandomNumber() :) - Tamás Szelei
(4) "In an infinite sequence of random numbers, you will see infinite sequences of the same number." -> is there mathematical proof for this? - StackedCrooked
(9) @StackedCrooked: It isn't true. But you would see arbitrarily long sequences of the same number. - Jason Orendorff
[+465] [2009-03-06 23:11:08] Can Berk Güder

Tour of Accounting

(135) A question based on XKCD with an answer based on Dilbert, this should be in the SO Hall of Fame - Neil N
(1) Holy Cows, nobody nails it like Scott Adms! - TheVillageIdiot
(36) Is the creature in the last frame the wrong color? Wouldn't he be the brown tour guide from frame one? - Brian Hasden
(5) Quick! Someone email Scott Adams! Which reminds me of a XKCD - - Mark Ransom
@Brian: Lighting in Hell can affect the perceived color of demons. What? It's not hell, it's accounting? I don't perceive the difference. - jmucchiello
@Brian: I think all the Dilberts were coloured after the fact for the website. There's lots of colouring errors around if you look. - McPherrinM
(3) Not only SO Hall of Fame, it should also go in the Internets Hall of Fame - Saggi Malachi
(12) @Brian that bugged me too -- I recolored the strip to fix, since it's one of my favorites - Jeff Atwood
(5) ZOMG! recolored by Jeff, 320 upvotes, I'm certainly glad I posted this. =) I only wish it wasn't CW. Jeff, can you give me 3200 rep as a christmas gift? =) - Can Berk Güder
Sir, you win the internet. - Greg Beech
@Can Berk Güder, you'll probably just have to settle for the badge. I wish I had an answer that was half as popular! - Mark Ransom
(1) @Jeff Atwood what about the tie? :p - Jeroen Dierckx
John Lennon RIP. - kenny
[+140] [2009-03-06 23:38:19] Svante

I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.

-- Charles Babbage

(6) That's mean. I upvoted you =) - Sergio Acosta
(42) Context: Babbage was asked (IIRC), "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" - Lucas Jones
[+97] [2009-01-20 18:56:19] Rob Z

The comment implies that who ever wrote it just rolled dice to get the number and is having it return the results of a dice roll. While the results of the dice roll are going to be truly random as opposed to pseudorandom [1], it misses the spirit of what a random number function is.

Thus, it is a joke.


(4) How do you keep a boring dud with no sense of humor in suspense? - Tim Schaeffer
(9) how do you apprehend the seventy one upvoters who approve of this? - wilhelmtell
(5) Sir, your proof is redundant. The fact that it is a comic strip is sufficient to establish that it is a joke. - intuited
(3) All we need to do now is to write a few billion random() functions: random1(), random2(), ..., random4294967295(). - muntoo
Even dice-rolling is not guaranteed to be truly random. In fact, scientists are not sure if there is anything random in the whole universe... - Halil Özgür
[+70] [2009-03-06 23:43:41] Will Hartung

I think you have to be really pedantic on this one, part of its flavor.

The function name "getRandomNumber()" basically means "Get a number that is random". Here, the number "4" was arrived at randomly, through a dice roll. Thus, 4 IS a random number. This function works PERFECTLY. It does exactly what is asked for, a random number.

This is a classic case of "exactly what you asked for, but not what you wanted".

(20) probably written by our outsourced development team :) - gbjbaanb
[+60] [2009-01-20 17:19:53] Jouni K. Seppänen

See "What Colour are your bits?" [1]. Randomness is not a property of a number, but of the process that produces the number.


(1) The article takes quite a bit until it reaches the "random" part, but it's a great article! - Joachim Sauer
(3) Your second sentence sums it up perfectly! - Beska
(5) Any number produced via a process is by definition not random. - Oorang
(3) @Oorang: hence, the famous von Neumann quotation: Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin. - Telemachus
*Nothing* is random. So there. Ha. - muntoo
[+60] [2009-01-20 18:29:35] Kent Fredric

From the outside of any given random number function, it's impossible to tell whether or not the numbers coming out of it are in fact random or not.

Seeing a stream of 4s come out would indicate that it's possibly not random, but alternatively, it could be a good time to get a lotto ticket (or a bad one, you can't tell).

Looking at the function's internals you can tell that it's not using a random source to produce those values, but without seeing that you'd never know for sure.

This is because it's perfectly legitimate for a random source to repeat. Lots of people have the funny concept the following are not random:

  • 4 4 4 4 4 4 4 4
  • 1 2 3 4 5 6 7 8
  • 8 7 6 5 4 3 2 1
  • 1 3 5 7 9 11 13

Sure, it's statistically unlikely that a random number generator will emit those patterns, but if it does, it's not a sign of not being random.

At the basic level, all sequences can be represented with bit patterns, 1s and 0s, and there are only so many combinations of that.

0        1

now the following sequences of length 2 are fully legitimate random sequences


And as a result, so is




A random number generator can easily produce these sequences without failing to be random. Sure, the longer the run is of bits it emits the more likely they are to change, and the longer a sequence is the less likely it is to be emitted by a random number generator, but that won't stop it from generating it.

I'll put it like this:

There are billions of people on the earth.

If you go out today and buy a lotto ticket, the chance of you winning is heavily against you.

But somebody wins, every day. Some people win more than once.

(29) "the longer the run is of bits it emits the more likely they are to change". No - if it's truly random it's a geometric distribution so probability of a change is independent of past behaviour. - Alabaster Codify
(6) The chance of a particular sequence of bits being produced does decrease as the length of the sequence increases, but you could say that about any "pattern", whether it seemed random or not. - Alabaster Codify
@Alabaster — I was just about to point those out myself. :-) - Ben Blank
Nicely done. +1 - KG.
(3) actually, true randomness is just as likely to generate 12345678 as anything else - the probability that you will get a certain pattern is only dependent on the number of combinations of numbers in a sequence that conform to that pattern. The whole point of true randomness is that any sequence is just as likely as any other (thanks to the fact that each number generated would have no bearing on any other), which is what confuses us humans so much! - RCIX
(14) I like Feynman's comment on this: "You know, the most amazing thing happened to me tonight. I was coming here, on the way to the lecture, and I came in through the parking lot. And you won't believe what happened. I saw a car with the license plate ARW 357. Can you imagine? Of all the millions of license plates in the state, what was the chance that I would see that particular one tonight? Amazing!" - TimK
(2) @RCIX- It is just as likely to generate 12345678 as anything else, but people consider 12345678 a special sequence, because they work with it far more often then any other randomly generated sequence. From this perspective, it's less likely to generate 12345678 than it is to generate /any sequence that people won't normally recognize/. There are far more sequences that "look" random than don't. - Rusky
You may want to look at stuff like the Chi-Square Test: - this is one mechanism to make meaningful tests of a Random Number Generation, by looking at it's distribution. - Michael Stum
@TimK, would "ARW 357" have a special meaning til Feynmann or was he just being ironic? - Thorbjørn Ravn Andersen
[+14] [2009-12-29 18:48:39] Dan Udey

Another example: The iPod had a problem with its shuffle feature, because it "wasn't random enough"; specifically, it would play the same artist several times in a row, or play a song more often, and so on. A lot of users complained about this, commenting that their iPod had a 'preference'. Maybe it liked Creedence (who wouldn't!) or refused to play Michael Jackson.

In reality, the problem was that it was TOO random, or at least it was as random as it could be (pseudo-random). Apple eventually added a slider to determine how 'random' you wanted songs to be. More 'random' actually meant less random, as it tracked which songs it had played recently and avoided similar songs.

Especially with songs, there are so many connections with other songs (same artist, same genre, same year, same album) that it's easy to see patterns that aren't actually there. The problem with randomness is that given a small enough set of unconnected possibilities, it won't seem random at all.

(3) more 'random' actually meant less random, nice ! - Preets
(1) There's probably an argument here that Apple didn't get their random function wrong in this case - they just incorrectly specified the problem. As a listener to a song-list, I don't actually want a totally random selection from my entire library. I want "a randomly select song from songs I haven't heard recently". - tardate
(1) Or rather "play this sequence (or collection) of songs in a random order", like shuffling a deck of cards, turning them over one by one and then discarding the displayed cards. - nikc
@tardate @nikc that's already how it worked. That was the algorithm that wasn't random enough. - Breton
[+13] [2009-03-06 23:03:09] Sarel Botha

The programmer misunderstood the requirement 'return a random number'.

While the number was random at design-time it has been hard-coded and so it will always return the same number. The requirement most certainly meant that the function should return a different number each time at runtime.

Basically, the programmer screwed up in a funny way.

I guess you could clarify the requirement 'return a random number' with 'at runtime' and maybe the programmer will get the function right. I doubt it though.

I don't think "it works".

(1) One could also argue that the function is to return a number, that can differ in different implementations. If it wants to. Note that the function name doesn't says anything about GENERATING a random number, just that is returns one. (In this implementation pregenerated.) - svinto
I once had a media player on my computer with this problem. Its random shuffle feature always used the same random order. - Matthew Scouten
There's a legitimate place for random number functions that aren't random. In an app I'm working on, to generate fake data, instead of new Random() I create new Random(0). I want random-looking data, but I want the same random-looking data each time I run the program so I know there's nothing funny going on with the program. If I run and suddenly see different data from the normal random-looking data, then I'll know I have a bug to track down. - Kyralessa
[+8] [2009-03-06 23:57:23] Chetan Sastry

Snippet seen in a project I previously worked:

 * Return a random number between 1 to 5
private int getRandom() {
    int rand = Random.nextInt(6); //number between 0 to 5 (inclusive)
    if (rand == 0)
    return rand;    

Unfortunately, it wasn't a lottery app else I'd be a millionaire by now.

(25) ogh... I felt a great disturbance in the Force, as if a random number of statisticians suddenly cried out in horror and were suddenly silenced. - Stefano Borini
I think I've seen the same thing in a real random number generation function. Mind you, it was dealing with floats between 0 and 1, so the chance of it being corrupted was only epsilon in 1. - Andrew Grimm
[+5] [2009-03-06 23:46:49] runrig

It works perfectly. With the caveat that every time the function is called, the programmer needs to roll the dice again, rewrite the function, and recompile the program. I assume that the number of dice and/or the probability distribution of the function are documented somewhere. Or not.

[+3] [2009-01-20 19:26:21] Jon Ericson

Technically, this function generates a random number sequence of just one number. More useful functions would generate longer sequences. Even more useful functions would generate different sequences when called multiple times. However, it could be argued that this function produces a "better" random number than more useful functions since, if the comment is to be believed, the sequence was generated by a universally acknowledged randomization procedure: a die roll.

Most "random number" generators are actually deterministic algorithms that are difficult to predict if you don't have access to some set of inputs. Traditionally, the needed input is called a seed and is a number a program provides to a random number generator. In turn, the seed is often generated by using the computer's internal clock so that each instance of the program will provide different results. But the sequences generated, while difficult to predict, are not random in the sense of non-deterministic. In fact, the deterministic nature of these sequences is the basis for many encryption techniques.

A truly random sequence would be both difficult to predict and non-deterministic (and meet certain statistical tests). Die rolls (as in the cartoon) are a good source for random sequences. But for most applications, the cost of generating a truly random sequence far outweighs the benefits compared to pseudo-random sequences.

[+2] [2009-12-29 20:10:00] wooptoo

You can use atmospheric noise like these guys [1] to generate true randomness.


[0] [2009-12-29 11:36:27] meduz

the algorithm is perfectly correct if you use it once and the user did not observe the source. if you want to use it another time (to return a random number over N), the XKCD recipe say:

  • build N different getRandomNumber() functions with the N different numbers,
  • delete N-1 programs randomly.

you have to be a lazy programmer to do something shorter and an over-zealous user to see the difference.

[0] [2009-01-20 19:58:00] devio

Thanks for re-opening the question ;)

I don't know why Preets thinks the function "works", and what is meant by "working".

A Random Number Generator should generate ...uhm... random numbers. Throw of dice can be seen as random number generation, as the resulting SEQUENCE of numbers appears to be random.

However, as has been pointed out by others, a single NUMBER cannot be "random".

The joke is that you take a random number generator, take one value of it, and claim that repeating that same result is still a random generator.

The function in the cartoon "generates" the same number over and over, is therefore a predictable sequence, and thus not random.

(6) Why do you think getRandomNumber() is a random number generator? It doesn't promise to generate a sequence of numbers, only that it returns a random number. It doesn't make any promises about returning a different number each time it is called. The 4 was chosen at random and thus is genuinely a random number. - Bryan Oakley