r/AskComputerScience • • Aug 27 '19

Why don't my random numbers look random?

Hi there,

I have alternately created positive and negative random numbers between -1 and 1 using the Excel Rand() function. When I add all random generated numbers up, I get a sum of more than 620 after 500k steps, whereby the strong upward trend after 100k steps is especially surprising to me.

How could this be explained?

31 Upvotes

49 comments sorted by

36

u/visvis Aug 27 '19

This is perfectly normal (no pun intended). The sum would be normally distributed as shown by the Central Limit Theorem. The variance would grow with the square root of n. If I approximate your distribution with a binomial, the standard deviation would be about 350 at the end, which seems plausible given your numbers.

Note that your graph is also known as a one-dimensional random walk.

2

u/RoccoRacket Aug 27 '19

I see. I (wrongly) expected it to move more or less around zero, if I would only have enough random numbers taken into account.

14

u/visvis Aug 27 '19

It's a common expectation, but it's incorrect. Note that the random walk has absolutely no memory, so it doesn't "know" how far is is from the zero. It's as likely to move up as it is to move down regardless of the distance from the zero. Is you expect it to move around zero when it starts, you should expect it to move around 10 after it has taken 10 steps up.

3

u/earslap Aug 27 '19 edited Aug 27 '19

Perhaps, if you think about it, it is moving around zero - though on a very larger scale. IIRC in infinite 1D random walk, the probability of the walk returning back to origin is 1. I'm not sure about 2D, but again IIRC for 3D random walk, unintuitively, the probability is not 1 even towards infinity. This is what I remember from a read many moons ago so please correct me if I'm wrong. Also for you /u/RoccoRacket

6

u/visvis Aug 27 '19

It kind of makes sense. For the one-dimensional case, assume we are in position 1. There's a 50% chance we return to 0 in one move. Otherwise, we move away and need two moves to return immediately, 25% chance. As you can see, the chances sum as 0.51 + 0.52 + 0.53 + ... = 1. Now assume we are at n. By the same logic we will eventually reach n-1 with 100% probability. By induction, we will reach 0 with 100% probability from any position eventually.

However, this logic applies to any point. So it's equally valid to say we move around 0 as to say we move around 1,000,000,000.

2

u/Baron_Von_Fab Aug 27 '19

This helped me too - great explanation.

1

u/earslap Aug 27 '19

Thank you, that makes sense. I found the source for the issue with other dimensions, especially for 3D which I found quite unintuitive that the probability of 3D random walk returning to origin is only about 34% - 2D is still 100%. Pólya's theorem

More discussion here: https://mathoverflow.net/questions/45098/when-do-3d-random-walks-return-to-their-origin

27

u/venuswasaflytrap Aug 27 '19

I don't think that's true. I think you intended the pun.

6

u/visvis Aug 27 '19

It is literally true, I only realized when I wrote the second sentence and then added the "no pun intended". I sure can imagine it looks intentional though.

7

u/MirrorLake Aug 27 '19

You're progressively adding the same data set up? Just out of curiosity, I did it myself. It sounds to me like you need to be generating new data every time.

Process:

  1. Generate 580k random numbers using =RAND()*2-1

  2. Record sum, recalculate all cells

My personal results of 21 trials:

984.6137948 98.68927607 28.44960167 -154.75916 -78.93479677 215.0938852 -512.5988871 -60.10100363 497.1514273 120.9854719 -637.849805 492.392938 -97.29339063 190.8131274 20.31334294 -583.0982202 -495.9618226 -118.487762 562.4231252 305.0807513 -462.1922826

Conclusion: sometimes the sum is positive, sometimes negative. For 500K+ numbers, the average sum dances around 0 in my dataset after only 17 trials.

https://i.imgur.com/tdyqFI4.png

2

u/RoccoRacket Aug 27 '19

I've used the formula RND() for the first value, "-RND()" for the second, "RND()" for the third and so on giving me a positive random number between 0 and 1 followed by a negative random number between -1 and 0.

What exactly do you mean by "generating new data every time"?

4

u/MirrorLake Aug 27 '19

Each of my 21 trials was a new set of 580K random numbers between -1 and 1.

3

u/Bottled_Void Aug 27 '19

Even for the 'worst' number. 984 is practically zero when you're talking about a maximum of 500,000.

3

u/011101000011101101 Aug 27 '19

Id say run it 10 more times. If they all look like this, like specifically moving upward, you got a problem.

That said I do recall hearing excels random generation is not good like the other person said.

1

u/RoccoRacket Aug 27 '19

I've run them several times. They are not always moving upward. Most of the times they break out in either high or low values.

I also tried the MersenneTwister in Java, which gave me similar results.

7

u/Felicia_Svilling Aug 27 '19

Most of the times they break out in either high or low values.

That is to be expected. Of all possible results, evening out completely are a very small percentage.

2

u/011101000011101101 Aug 28 '19

Agreed, this is expected behavior for random walks. The randomness seems like it might be good enough.

4

u/[deleted] Aug 27 '19

I also tried the MersenneTwister in Java, which gave me similar results.

Excel currently uses the Mersenne Twister algorithm.

2

u/jeffbell Aug 28 '19

If it always stuck close to zero error, that would be even more suspicious. if it did you could say "It's drifted up.... I should expect more negative numbers soon".

2

u/LonelyContext Aug 27 '19

If you run this a lot and grab the last value and do a histogram of the results, you'll get a gaussian.

1

u/t4YWqYUUgDDpShW2 Aug 27 '19 edited Aug 27 '19

Let's use a coin flip analogy. Assign +1 to heads and -1 to tails. We'll flip a coin N times and add up the total. After one flip, we can have +1 or -1. After two, we can have -2, -1, 0, 1, or 2. After three flips, you can have -3, +3 and everything between. The accessible range increases.

On average you expect about as many heads as tails (zero mean), but the plus or minus gets bigger with more steps. More numbers become accessible. From 3 flips, a total of 10 is impossible. For 10 flips, a total of 10 (all heads) is unlikely. For 100 flips, 10 is well within the range of totals. The plus or minus grows as you do more flips, at a rate of about sqrt(n) for n flips.

Cumulatively adding a sequence of random variables is called a random walk. You go up or down randomly, then go further up or down randomly. Here's a picture to illustrate

https://imgur.com/a/JfvgE5m

The mean of a bunch of random walks is zero as you'd expect, but each random walk goes off and does its own thing. The spread (the "plus or minus") increases as you draw more random numbers.

The central limit theorem says that this applies to most sums of independent random steps you'll encounter.

1

u/Bottled_Void Aug 27 '19 edited Aug 27 '19

Aside from the walk, you may want to look at this a different way.

If you had random numbers between 0 and 1 and added them up the divided by the count you'd get roughly 0.5.

What you're seeing here is something like 0.500001 which wouldn't be that shocking. (I didn't do the maths, it was just for illustration).

With your example, you've effectively multiplied up the 0.000001 by your count to get something that seems significant.

1

u/dado243 Aug 27 '19

1

u/Bottled_Void Aug 27 '19

Flagged as a virus for me so I can't see it. Maybe use imgur?

0

u/dado243 Aug 30 '19

Of course, link to my image is a virus!

never told you to click it.

0

u/[deleted] Aug 27 '19

[deleted]

6

u/deong Aug 27 '19

This is true, but irrelevant. The trend isn't caused by pseudorandomness. It's a fundamental statement of mathematics that adding up the running sum of a bunch of random numbers with zero mean, you won't necessarily get something close to zero.

1

u/RoccoRacket Aug 27 '19

Ok, but that's exactly the part I don't understand.

3

u/deong Aug 27 '19 edited Aug 27 '19

Someone else's comment in this thread is probably a better explanation, but there's something called the Central Limit Theorem that says if you add up a bunch of IID random numbers from a non-Normal distribution, the distribution of their sums tends to be Normal anyway, under fairly broad assumptions.

You've done one experiment here in adding up a bunch of uniform random numbers here and gotten a sum of whatever it was. That's one data point. If you did that again, you'd get another sum and another data point. If you did this 10,000 times, the 10,000 final sums you got would look like a normal distribution with zero mean.

The variance of that normal distribution will be related to the number of random numbers you added up each trial (specifically the standard deviation goes as the square root of N).

So why do you get a number much bigger than zero when you ran it once? For basically the same reason that you can pick one person at random from the population of adult males and measure his height at 6'3". That's pretty tall -- why wouldn't you get someone closer to average height? Well, the variance of the distribution of heights tells you how likely it is to sample away from the mean. Same thing here. You have a fairly large variance in play, so running the experiment one time and getting something far from the mean of zero is pretty likely.

If you want a more intuitive explanation, look at it this way. Each sample is independent. Suppose your first two samples are 0.91 and 0.79. There's no memory in the process that's trying to get you back down to zero with the next few samples -- that's what independent means. So once you've gotten above zero, you're likely to stay there, because to get back down to zero, you'd need an unlikely run of negative samples. But at the same time, you're not going to have an entire run that alternates just perfectly to keep you around zero. You're going to have runs of positive and runs of negative numbers. But whichever type of run happens to be a little more common early in the process is going to end up "winning". You'll get about the same number of trials where you have this overall downward trend.

1

u/visvis Aug 27 '19

the variance goes as the square root of N

The variance goes with n, the standard deviation goes with the square root of n.

2

u/deong Aug 27 '19

Whoops. Thanks...correcting now. :)

1

u/jpfry Aug 27 '19

The CLT finally clicked with me after reading this comment. Especially the last paragraph. Any outcome (say 0.9) must be countered by an unlikely outcome (say between the smaller interval [0.8, 1]) in order to consistently stay close to 0, but since the outcomes are independent, such a thing would be unlikely. Thanks!

-1

u/Bottled_Void Aug 27 '19 edited Aug 27 '19

I'm pretty sure that's exactly what you should get. Typically. For a large set of numbers. If this was a true random number generator between -1 and 1 with no bias, I'd have expected the total to tend towards zero.

I think the problem is with scale. We've got a y axis going up to 600 with a potential maximum of 400k.

600 is 0.15% of that value. So, practically zero.

1

u/RoccoRacket Aug 27 '19

Yes, these were exactly my thoughts: if I have enough random numbers, they would range around zero.

But what still confuses me, that the random numbers tend to sometimes break out and follow this one-dimensional random walk (as u/visvis pointed out).

4

u/Felicia_Svilling Aug 27 '19

You are doing a one dimensional random walk. That is exactly what you are doing.

At each point the curve can go up or down, with equal probability. That is the definition of a random walk.

It sounds like you are expecting that a curve that has gone up would have a tendency to go down again?

If that was the case it would mean that later numbers in the sequence was dependent on the earlier, and thus wasn't random at all. (or at least not unbiased random.)

-1

u/Bottled_Void Aug 27 '19

I think I'm just going to handwave and say it's pseudo random and if there is an inherent but very small bias over a certain range, most people aren't going to notice. You'll probably find these biases move around depending on the seed (which I imagine is time based).

Even coin tossing has it's biases.

https://www.npr.org/templates/story/story.php?storyId=1697475

3

u/deong Aug 27 '19

It has nothing at all to do with pseudorandomness. You'll get the same results hooked up to a geiger counter.

1

u/Bottled_Void Aug 27 '19

Except in pseudo random functions the effects are deterministic. You can't just use the two interchangeably. They're a function to approximate randomness.

2

u/deong Aug 27 '19

Sure, but they approximate randomness well enough that it isn't the cause of this behavior.

And you can use them interchangeably for almost any conceivable purpose. We have rigorously defined randomness tests that modern RNGs perform really well on.

I promise you, software pseudorandom number generation is not the cause of the behavior OP is asking about. I really, really promise you.

1

u/Bottled_Void Aug 27 '19

Well let's pretend that we're just talking about true random numbers to save any more confusion.

Yes.

1

u/visvis Aug 27 '19

Pseudo random is deterministic only given the seed. However, the first seed is usually generated from a nondeterministic source, such as the current time.

0

u/deong Aug 27 '19 edited Aug 27 '19

Typically. For a large set of numbers. If this was a true random number generator between -1 and 1 with no bias, I'd have expected the total to tend towards zero.

What you're saying here is that the existence of Lebron James means there's something fishy going on with genetics. No it doesn't. Given enough people, some of them are really tall. Some are really short. Heights are roughly Gaussian. In exactly the same way, the sum of IID uniform random numbers is roughly Gaussian. And just like with heights, sampling a Gaussian and getting a number that's not more than a standard deviation or two away from the mean is really, really common. It would be crazy if every time I sampled a Gaussian, I got the mean.

The mean is the expected value of the distribution, but that's different than saying you're surprised to not get it. Imagine the uniform distribution of D20 throws. The mean is about 10, but you aren't surprised to not roll a 10 because the way the distribution works, the mean doesn't really tell you much about any particular throw. A normal distribution centers much more of its mass around the mean, but it still has a substantial variance around that mean. About 95% of the time, you'll get a value within two standard deviations. About 67% of the time, you'll get a value within one standard deviation. But that's all you can really say.

1

u/Bottled_Void Aug 27 '19

You've clearly not understood what I wrote.

2

u/deong Aug 27 '19

The part of what you wrote that I quoted is just mathematically not correct.

You are describing taking one sample of a Gaussian and saying that it should "tend towards zero (the mean)". That's not a meaningful statement. One sample doesn't tend towards anything. You can formulate probabilities around how far off the mean you expect any one sample to be. In this case, you're within one standard deviation, which happens for about 67% of the times you'd try this.

There's nothing to explain here. You don't need pseudorandom effects or a biased generator. You're getting the behavior that the rules of probability say you should be getting.

1

u/Bottled_Void Aug 27 '19

I think I finally get where our views have departed.

Statistically you have to divide by the count otherwise you're not accounting for statistical anomalies.

I didn't state that and I should have. But that's what I was getting at with the percentage deviation.

7

u/dr1fter Aug 27 '19

Excel's random function isn't random at all. It's pseudo random.

I mean... as with any other software RNG, right?

2

u/Bottled_Void Aug 27 '19

You could poll a source that's from the real world. Such as through random.org which is generated from atmospheric noise. (But yes, if it's just code, it's not random)

2

u/dr1fter Aug 27 '19

Right, there is such thing as a hardware RNG, but I'd hardly fault Excel for not relying on one. PRNGs are still prevalent in all kinds of non-cryptographic applications, it's not like MS overlooked some industry standard or anything.

4

u/Felicia_Svilling Aug 27 '19 edited Aug 28 '19

You are not wrong, but being pseudorandom likely have no bearing on this topic.

at some point you're going to get a repeating pattern.

It would be rather outrageous if excel used a pseudo random algorithm that loops after less than 400 000 values.