Skip to content

Instantly share code, notes, and snippets.

@Const-me
Created February 20, 2022 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Const-me/4b9b4372ba37c03feaf2f1475b10845e to your computer and use it in GitHub Desktop.
Save Const-me/4b9b4372ba37c03feaf2f1475b10845e to your computer and use it in GitHub Desktop.
static class Program
{
// Make a random transaction between two people.
static void randomTransaction( ref int to, ref int from )
{
const int transactionAmount = 5;
int amount = Math.Min( transactionAmount, from );
from -= amount;
to += amount;
}
// Sample from sorted array at the specified location
static int sample( this int[] arr, double where )
{
int idx = (int)Math.Round( where * ( arr.Length - 1 ) );
return arr[ idx ];
}
static void Main()
{
// 100k people
const int countPeople = 100000;
// 1G random transactions
const long countTransactions = 1000000000;
// Initialize the array with equal number $100
int[] arr = Enumerable.Repeat( 100, countPeople ).ToArray();
Random random = new Random();
// Run a large count of 100% fair random transactions
for( long i = 0; i < countTransactions; i++ )
{
int a = random.Next( countPeople );
int b = random.Next( countPeople );
randomTransaction( ref arr[ a ], ref arr[ b ] );
}
// Print some wealth distribution figures
Array.Sort( arr );
IEnumerable<(string, double)> outputs()
{
yield return ("10%", 0.1);
yield return ("20%", 0.2);
yield return ("Median", 0.5);
yield return ("80%", 0.8);
yield return ("90%", 0.9);
yield return ("99%", 0.99);
yield return ("99.9%", 0.999);
}
foreach( (string label, double loc) in outputs() )
Console.WriteLine( "{0}: ${1}", label, arr.sample( loc ) );
// Print top 10 elements from the array
var top10 = arr.Reverse().Take( 10 ).Select( a => $"${ a }" );
Console.WriteLine( "Top 10 list: {0}", string.Join( ", ", top10 ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment