Skip to content

Instantly share code, notes, and snippets.

@ShacharWeis
Last active February 18, 2018 13:13
Show Gist options
  • Select an option

  • Save ShacharWeis/f84b2ceb1a593ee78e82f8d392a788ae to your computer and use it in GitHub Desktop.

Select an option

Save ShacharWeis/f84b2ceb1a593ee78e82f8d392a788ae to your computer and use it in GitHub Desktop.
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;
}
}
}
@ShacharWeis
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment