private static void Main() { /* * // Attempt to calculate probabilities * { * const int bits = 42; * const int servers = 100; * const int rate = 64; * * // Probability on one (rate-exhausted) timestamp for one server to have NO collisions with one other server * var prob = 1.0; * for (var i = 0UL; i < rate; i++) * { * var probForI = ((1UL << bits) - rate - i) / (double)((1UL << bits) - i); * prob *= probForI; * } * * // To the power of the number of distinct server pairs * // Gives us the probability that there are no collisions on that timestamp among all of the servers * prob = Math.Pow(prob, servers * (servers - 1) / 2); * * // Probability of one or more collisions on one (rate-exhausted) timestamp * // We will pretend this is the probability of just one collision, although it is technically one OR MORE * var collisionProb = 1 - prob; * * var collisionsPerId = collisionProb / (servers * Rate); * * Console.WriteLine(collisionsPerId); * var idsPerCollision = 1 / collisionsPerId; * * Console.WriteLine($"Calculated 1 collision in {(ulong)idsPerCollision:#,##0} IDs."); * }*/ // Calculate average maximum generation rate { var tempResults = new List <int>(); for (var i = 0; i < 100; i++) { var rate = 1; var previousValue = RandomSequence6.Create(); while (previousValue.TryAddRandomBits(RandomSequence6.Create(), out previousValue)) { rate++; } tempResults.Add(rate); } var lowRate = tempResults.Min(); var highRate = tempResults.Max(); var avgRate = tempResults.Average(); Console.WriteLine($"Low {lowRate}, high {highRate}, avg {avgRate}"); } var logInterval = TimeSpan.FromSeconds(10); var sw = Stopwatch.StartNew(); #pragma warning disable CS4014 // Deliberately unawaited background task LogAtIntervals(sw); // Unawaited task #pragma warning restore CS4014 while (true) { IterationCount++; Parallel.For(0, Parallelism, ProcessArray); FindDuplicates(); ResetGenerationCounts(); } }
/// <summary> /// <para> /// Pure function (although the random number generator may use locking internally). /// </para> /// <para> /// Returns a new 48-bit (6-byte) random sequence. /// </para> /// </summary> private RandomSequence6 CreateRandomSequence() { return(RandomSequence6.Create()); }