Skip to content

Instantly share code, notes, and snippets.

@kezzyhko
Created August 15, 2024 07:29
Show Gist options
  • Select an option

  • Save kezzyhko/90dd72e827c6494acb75bc56d28c19b7 to your computer and use it in GitHub Desktop.

Select an option

Save kezzyhko/90dd72e827c6494acb75bc56d28c19b7 to your computer and use it in GitHub Desktop.
Quick and dirty code to test random distributions
using System;
using System.Collections.Generic;
public class HelloWorld
{
public static void Main(string[] args)
{
var random = new Random();
var distribution = new Dictionary<int, int>();
var SCALE = 1000;
for (var i = 1; i <= 1000*SCALE; i++)
{
var rand = random.NextNormalInt(0, 10);
var n = distribution.GetValueOrDefault(rand, 0);
distribution[rand] = n+1;
}
for (var i = -30; i <= 30; i++)
{
var n = distribution.GetValueOrDefault(i, 0) / SCALE;
Console.WriteLine(new String('|', n));
}
}
}
public static class RandomExtensions
{
public static int NextNormalInt(this Random random, float mean = 0, float standardDeviation = 1)
{
var normalFloat = random.NextNormalFloat(mean, standardDeviation);
var normalInt = (int)Math.Round((double)normalFloat);
return normalInt;
}
// https://stackoverflow.com/a/218600/6702274
public static float NextNormalFloat(this Random random, float mean = 0, float standardDeviation = 1)
{
var u1 = 1 - random.NextDouble();
var u2 = 1 - random.NextDouble();
var randNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
return mean + standardDeviation * (float)randNormal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment