Skip to content

Instantly share code, notes, and snippets.

@dsonbill
Created April 2, 2019 09:18
Show Gist options
  • Save dsonbill/6220dadecce3aae2515ba492944e6ed9 to your computer and use it in GitHub Desktop.
Save dsonbill/6220dadecce3aae2515ba492944e6ed9 to your computer and use it in GitHub Desktop.
Googol Game as described by Vsauce2 at https://www.youtube.com/watch?v=OeJobV4jJG0
using System;
using System.Collections.Generic;
namespace GoogolGame
{
class Program
{
const decimal e = 2.71828M;
static void Main(string[] args)
{
int boxes = 1000;
int iterations = 500;
List<bool> results = new List<bool>();
int totalIterations = 0;
List<decimal> selections;
Random rnd;
int tries;
while (true)
{
//Create a new Random object
rnd = new Random();
// Number of tries to sample from - number of boxes / euler's number
tries = decimal.ToInt32(boxes / e);
// Iterate many times
for (int i = 0; i < iterations; i++)
{
// Pick out random selections for each box
selections = new List<decimal>();
for (int x = 0; x < boxes; x ++)
{
// Add random selection to a list
selections.Add(rnd.NextDecimal());
}
// Find the highest number in x tries from above
decimal highest = 0;
for (int x = 0; x < tries; x++)
{
// If the current number is higher than our highest, it's our new highest
if (selections[x] > highest)
{
highest = selections[x];
}
}
// Look for a higher card after picking our sample group
int stoppingPoint = tries;
for (int x = tries; x < selections.Count; x++)
{
// If we find one, stop looking
if (selections[x] > highest)
{
highest = selections[x];
stoppingPoint = x;
break;
}
}
//Implied Victory
bool lost = false;
// Did we find a higher card? If not, we lost
if (stoppingPoint == tries)
{
lost = true;
}
else
{
// Check from our stopping point on and see if anything is higher
for (int x = stoppingPoint; x < selections.Count; x++)
{
// We lost, found something higher
if (highest < selections[x])
{
lost = true;
break;
}
}
}
// Store as a win
results.Add(!lost);
}
// Increase iteration multiplier
totalIterations++;
// Add up wins
int wins = 0;
foreach(bool result in results)
{
if (result) wins++;
}
// Print Results
// Console.Clear();
Console.WriteLine("Win ratio is " + (100 * (((decimal)1 / (iterations * totalIterations)) * wins)) + "%");
// Console.Write("Press any key to try again...");
// Console.ReadKey();
// Remove this line if running on your computer at home
//break;
}
}
}
// From https://stackoverflow.com/a/609529
static class Xtend
{
/// <summary>
/// Returns an Int32 with a random value across the entire range of
/// possible values.
/// </summary>
public static int NextInt32(this Random rng)
{
int firstBits = rng.Next(0, 1 << 4) << 28;
int lastBits = rng.Next(0, 1 << 28);
return firstBits | lastBits;
}
public static decimal NextDecimal(this Random rng)
{
byte scale = (byte)rng.Next(29);
bool sign = rng.Next(2) == 1;
return new decimal(rng.NextInt32(),
rng.NextInt32(),
rng.NextInt32(),
sign,
scale);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment