diff --git a/snippets/csharp/System/Random/Overview/next3.cs b/snippets/csharp/System/Random/Overview/next3.cs index d22fc1af2ad..cbc7fc6c88a 100644 --- a/snippets/csharp/System/Random/Overview/next3.cs +++ b/snippets/csharp/System/Random/Overview/next3.cs @@ -1,30 +1,41 @@ -using System; +using System; public class Example { - public static void Main() - { - // - Random rnd = new Random(); + public static void Main() + { + // + Console.Write("Number of random numbers to generate: "); - Console.WriteLine("Generating 10 random numbers:"); + string? line = Console.ReadLine(); + Random rnd = new Random(); - for (uint ctr = 1; ctr <= 10; ctr++) - Console.WriteLine($"{rnd.Next(),15:N0}"); + if (!int.TryParse(line, out int numbers) || numbers <= 0) + { + numbers = 10; + } - // The example displays output like the following: - // - // Generating 10 random numbers: - // 1,733,189,596 - // 566,518,090 - // 1,166,108,546 - // 1,931,426,514 - // 1,532,939,448 - // 762,207,767 - // 815,074,920 - // 1,521,208,785 - // 1,950,436,671 - // 1,266,596,666 - // - } + for (uint ctr = 1; ctr <= numbers; ctr++) + Console.WriteLine($"{rnd.Next(),15:N0}"); + + // The example displays output like the following when asked to generate + // 15 random numbers: + // Number of random numbers to generate: 15 + // 367 920 603 + // 1 143 790 667 + // 1 360 963 275 + // 1 851 697 775 + // 248 956 796 + // 1 009 615 458 + // 1 617 743 155 + // 1 821 609 652 + // 1 661 761 949 + // 477 300 794 + // 288 418 129 + // 425 371 492 + // 1 558 147 880 + // 1 473 704 017 + // 777 507 489 + // + } }