Skip to content

Instantly share code, notes, and snippets.

@ociaw
ociaw / BiasedRng.csx
Created May 28, 2020 04:17
Demonstrates the high amount of bias in .NET's random number generator, System.Random.
var rng = new Random(42);
var sampleSize = 10_000_000;
var numbers = Enumerable.Repeat(0, sampleSize).Select(_ => rng.Next(0, Int32.MaxValue));
var heads = numbers.Count(x => x % 2 == 1);
Console.WriteLine($"Heads: {heads}, tails: {sampleSize - heads}");
// Output: Heads: 5,036,860, tails: 4,963,140
var popStdDev = Math.Sqrt(0.5 * 0.5);
var popMean = 0.5;
var sampleMean = (Double)heads / sampleSize;
@ociaw
ociaw / Benchmark.cs
Created October 22, 2019 05:14
A crappy struct benchmark
/*
* A quick and dirty micro benchmark on passing a class vs a struct vs a struct by reference
*
* This is not necessarily a good benchmark. The code will likely perform much differently
* under a more realistic scenario.
*
* Summary:
* | Method | N | Mean | Error | StdDev | Rank |
* |------------------------------ |-------- |------------------:|--------------:|--------------:|-----:|
* | BenchmarkMediumClass | 1 | 0.6700 ns | 0.0017 ns | 0.0016 ns | 2 |