views:

55668

answers:

15
+116  Q: 

XKCD - Random Number

Could someone explain if and why the following random number function from XKCD 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.

+54  A: 

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

Jouni K. Seppänen
The article takes quite a bit until it reaches the "random" part, but it's a great article!
Joachim Sauer
Your second sentence sums it up perfectly!
Beska
Any number produced via a process is by definition not random.
Oorang
@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
+56  A: 

From the outside of any given random number function, it's impossible to tell with a small amount of output 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). Not enough data.

Looking at the function's internals you can tell that it's not using a random source to produce those few 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 12 14

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

00
01 
10 
11 

And as a result, so is

00000000000000000000
11111111111111111111

and

10101010101010101010

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.

Kent Fredric
"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
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
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
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
@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: http://en.wikipedia.org/wiki/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
+90  A: 

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, it misses the spirit of what a random number function is.

Thus, it is a joke.

Rob
How do you keep a boring dud with no sense of humor in suspense?
Tim Schaeffer
how do you apprehend the seventy one upvoters who approve of this?
wilhelmtell
Sir, your proof is redundant. The fact that it is a *comic strip* is sufficient to establish that it is a joke.
intuited
+2  A: 

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.

Jon Ericson
A: 

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.

devio
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
+12  A: 

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".

sjbotha
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
+426  A: 

Tour of Accounting

Can Berk Güder
A question based on XKCD with an answer based on Dilbert, this should be in the SO Hall of Fame
Neil N
Holy Cows, nobody nails it like Scott Adms!
TheVillageIdiot
Is the creature in the last frame the wrong color? Wouldn't he be the brown tour guide from frame one?
Brian Hasden
Quick! Someone email Scott Adams! Which reminds me of a XKCD - http://xkcd.com/386/
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
Not only SO Hall of Fame, it should also go in the Internets Hall of Fame
Saggi Malachi
http://gamesbyemail.com/News/DiceOMatic
Kevin
@Brian that bugged me too -- I recolored the strip to fix, since it's one of my favorites
Jeff Atwood
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
@Jeff Atwood what about the tie? :p
Jeroen Dierckx
John Lennon RIP.
kenny
+130  A: 

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

-- Charles Babbage

Svante
That's mean. I upvoted you =)
Sergio Acosta
Context: Babbage was asked (IIRC), "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?"
Lucas Jones
+60  A: 

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".

Will Hartung
probably written by our outsourced development team :)
gbjbaanb
+5  A: 

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.

runrig
+5  A: 

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)
        rand++;
    return rand;    
}

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

Chetan Sastry
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
A: 

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.

meduz
A: 

This is misleading. This would be the right implementation.

int getRandomNumber(){

  getHumantoRolldice();
  return captureOutputofRolldiceEvent();

}
mathaix
I think you misunderstood the joke.
dreamlax
+11  A: 

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.

Dan Udey
more 'random' actually meant less random, nice !
Preets
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
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
+1  A: 

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

wooptoo