Skip to content

ZhongUncle/Prove-CLT-in-CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Prove-CLT-in-CPP

This project is inspired by 3Blue1Brown. When I watch the But what is the Central Limit Theorem?, I notice the dice example is a good trial for programming to "draw" a normal distribution curve. So I wrote C++ codes and use PPM format image to prove it and try more probability to draw diffderent images. The results of different conditions like magic of nature and math. I want more people to be able to use computer to see the "magic" by themselves.

If you can read Chinese, there is a blog to describe the details and some trials (two URL are same, choose which you can fast access):

Please choose your language to view according url.

If you are curious about the meaning of some codes, you can read the blog, the comments of code will answer your question. If you can't read Chinese and you don't trust the auto-tranlate, please ask me.

Usage

size of image

The size of PPM image control by width and height in line 28-19. If you change how many times added, the number of trail samples or probability, you should change the size to display entire chart.

how many times added

In line 51 you can change the number of times added.

number of trail samples

In line 48 you can change the number of trail samples.

probability of elements

In source code, there is an array a[] control probability of different elements. By default, the a[] is {1, 2, 3, 4, 5, 6}, it means each element have same probability, 1/6.

If you change the array, the probability of elements will change. For example, if the array is {1, 2, 3, 4, 5, 6, 6, 6, 6, 6}, the probability of 6 is 1/2 (5/10), probability of other elements is 1/10. I leave a extreme situation in comment, the probability of 1 is 0.95 and others are 0.01, you can try it. The image will be like:

Build and run

After setting, you can run this command:

$ g++ pclt.cpp && ./a.out 

It will generate a PPM image called output-image.ppm. The directory will be like:

You can see a nice image of normal distribution curve.

LICENSE

If you want to use codes and contents outside of education or personal usage (including blog), please credit the original.

About

Prove Central Limit Theorem(CLT) in C Language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages