Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » probability and graphs

Credits go to HoHo for helping out!
This thread is locked; no one can reply to it. rss feed Print
probability and graphs
Richard Phipps
Member #1,632
November 2001
avatar

Just a quick question. Say we have a variable which we calculate by adding up two random numbers between 0-100 and then dividing the total by two.

If we then count the number of times each final number (0-100) comes up after a few thousands iterations and plot it on a graph, would the output be a triangle shape or a bell shaped curve?

:)

GullRaDriel
Member #3,861
September 2003
avatar

It depend of your random number generator algorythm.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Richard Phipps
Member #1,632
November 2001
avatar

Well, let's suppose for the sake of argument that it's a totally random output.

HoHo
Member #4,534
April 2004
avatar

see attachment.

I took 2x512 random numbers, added them up and took the average as you said. Sorted the resulting numbers and created a chart out of it. If the line would be linear it would be a linear distribution. From what I can rad out of it it really is a bell distribution.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Richard Phipps
Member #1,632
November 2001
avatar

Why thank you! I didn't expect someone to go to that much trouble. I just thought some of the math people here already knew the answer from their studies.

Cheers!
:)

Johan Halmén
Member #1,550
September 2001

Hm, let's throw two dice.

result     probability
1          0
2          1/36
3          2/36
4          3/36
5          4/36
6          5/36
7          6/36
8          5/36
9          4/36
10         3/36
11         2/36
12         1/36

Now wouldn't that be a triangle? You need more dice to get the bell. One die gives a line, two dice give a triangle, more dice twist the triangle towards the bell.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Richard Phipps
Member #1,632
November 2001
avatar

Johan: You are not dividing the result by 2 are you?

Evert
Member #794
November 2000
avatar

Quote:

would the output be a triangle shape or a bell shaped curve?

It will approach a bell shaped if the initial distribution is flat.
Sketch of proof: near the top of the distribution, the number of ways to create the maximum value remains the same (i.e. 1). The same holds for the minimum value you can generate.
However, near the middle of the distribution, there are more ways to generate the result (you can write down the number of permutations that yields the same result, but I can't be bothered). You can even go further and take the limit of N measurements (instead of 2) and show that the shape of the distribution tends to the normal distribution. You can test this easily with a couple of dice.

Punchline: if you sum two distributions, the result is a new and different distribution (a sum of flat distributions is no longer flat, nor is the a of normal distributions normal).

Johan Halmén
Member #1,550
September 2001

Why should I divide it? Dividing is just scaling linearly, right? Gosh, now I don't believe in myself anymore. Got to code that myself. Hate you if I was right. Hate you if I was wrong, too.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Richard Phipps
Member #1,632
November 2001
avatar

miran
Member #2,407
June 2002

Compile this:

1#include <allegro.h>
2 
3int main() {
4 allegro_init();
5 set_color_depth(32);
6 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
7 install_keyboard();
8 
9 int num[640];
10 for (int i=0; i<640; i++) {
11 num<i> = 0;
12 }
13 
14 srand(time(0));
15 int max = 0;
16 for (int i=0; i<1000000; i++) {
17 int x = rand()%640;
18 int y = rand()%640;
19 int z = (x + y)/2;
20 ++num[z];
21 if (num[z] > max) {
22 max = num[z];
23 }
24 }
25 
26 clear_to_color(screen, makecol(212,212,212));
27 int col2 = makecol(192,64,64);
28 for (int i=0; i<640; i++) {
29 int y1 = SCREEN_H - num<i>*SCREEN_H/max;
30 int y2 = SCREEN_H;
31 vline(screen, i, y1, y2, col2);
32 }
33 
34 readkey();
35}
36END_OF_MAIN()

It will draw a triangle.

--
sig used to be here

Evert
Member #794
November 2000
avatar

Quote:

Why should I divide it? Dividing is just scaling linearly, right?

Yes. You should divide it if you want to keep the same normalisation.

Arthur Kalliokoski
Second in Command
February 2005
avatar

To say it all a different way, let's throw the dice again.

To throw a 2 both dice have to fall on one's, no exceptions.

But to get a 7, you could have a "1+6", "2+5" "3+4" "4+3" "5+2" or "6+1".

Well, I suppose listing the result to get 2 would take "1+1" and "1+1" for symmetry.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Evert
Member #794
November 2000
avatar

To clarify what I said in my first post:
The sum of N flat distributions tends to a normal distribution as N tends to infinity.

X-G
Member #856
December 2000
avatar

Quote:

Yes. You should divide it if you want to keep the same normalisation.

But we're only interested in the distribution, which shouldn't change by scaling it.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Evert
Member #794
November 2000
avatar

True, but it's easier to compare the distributions if they're both normalised.

Richard Phipps
Member #1,632
November 2001
avatar

So how do you get a bell shaped curve with probabilites? ???

(None of this is really relevant I guess.. Just curious)

Matthew Leverton
Supreme Loser
January 1999
avatar

X-G
Member #856
December 2000
avatar

Quote:

So how do you get a bell shaped curve with probabilites?

Box-Muller transform.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Richard Phipps
Member #1,632
November 2001
avatar

Johan Halmén
Member #1,550
September 2001

miran said:

It will draw a triangle.

Thanks, miran. Now I don't have to hate anyone.

[added]

Arthur said:

Well, I suppose listing the result to get 2 would take "1+1" and "1+1" for symmetry.

No. There's only one 1+1 case.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: