Last active
February 18, 2018 13:13
-
-
Save ShacharWeis/f84b2ceb1a593ee78e82f8d392a788ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace EulerNumberGenerator | |
| { | |
| class Program | |
| { | |
| static Random rand = new Random(); | |
| static void Main(string[] args) | |
| { | |
| double sum = 0; | |
| int amount = int.MaxValue; | |
| for (int i = 0; i < amount; i++) | |
| { | |
| int picks = DoRandomPicks(); | |
| sum = sum + picks; | |
| double avg = sum / (i + 1); | |
| Console.WriteLine("Avg:" + avg + " Error: " + (avg - Math.E)); | |
| } | |
| } | |
| static int DoRandomPicks() | |
| { | |
| int pickCounter = 0; | |
| double sum = 0; | |
| while (sum < 1) | |
| { | |
| double r = rand.NextDouble(); | |
| sum = sum + r; | |
| pickCounter++; | |
| } | |
| return pickCounter; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
Avg:2.70660032651792 Distance:-0.0116815019411254
Avg:2.70662313432836 Distance:-0.0116586941306869
Avg:2.70664593859308 Distance:-0.0116358898659632
Avg:2.70666873931292 Distance:-0.0116130891461275
Avg:2.70669153648869 Distance:-0.0115902919703532