{}
run-icon
Main.cs
// Online C# Editor for free // Write, Edit and Run your C# code using C# Online Compiler using System; using System.Collections.Generic; using System.Linq; public class HelloWorld { static void Main(string[] args) { // Range of numbers (as in your example: from 1 to 100) int minNumber = 1; int maxNumber = 100; int rangeCount = maxNumber - minNumber + 1; // Batch sizes to demonstrate the Law of Large Numbers // Added more steps to make the convergence visually clear int[] batches = { 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 6000000 }; Random random = new Random(); Console.WriteLine("Random Distribution Convergence Demo"); Console.WriteLine("Demonstrating the Law of Large Numbers\n"); Console.WriteLine($"Number Range: {minNumber}-{maxNumber}"); Console.WriteLine($"Expected Probability per Number: {100.0 / rangeCount:F4}%"); Console.WriteLine(new string('-', 70)); foreach (int batchSize in batches) { // Dictionary to count frequency of each number Dictionary<int, int> counts = new Dictionary<int, int>(); for (int i = minNumber; i <= maxNumber; i++) { counts[i] = 0; } // Generate random numbers for (int i = 0; i < batchSize; i++) { int num = random.Next(minNumber, maxNumber + 1); counts[num]++; } // Calculate percentages and find min/max deviations double expectedPercent = 100.0 / rangeCount; double minPercent = double.MaxValue; double maxPercent = double.MinValue; // Track which numbers had min/max percentages for display int minNum = 0; int maxNum = 0; foreach (var kvp in counts) { double percent = (double)kvp.Value / batchSize * 100.0; if (percent < minPercent) { minPercent = percent; minNum = kvp.Key; } if (percent > maxPercent) { maxPercent = percent; maxNum = kvp.Key; } } double spread = maxPercent - minPercent; // Output results Console.WriteLine($"Batch: {batchSize,7} rolls"); Console.WriteLine($" Min %: {minPercent,7:F4}% (Number {minNum,3})"); Console.WriteLine($" Max %: {maxPercent,7:F4}% (Number {maxNum,3})"); Console.WriteLine($" Spread (Max - Min): {spread,7:F4}%"); Console.WriteLine(new string('-', 70)); } Console.WriteLine("\nAs the number of rolls increases, the spread decreases."); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
Output