Skip to content

Commit 394571c

Browse files
authored
Actualize snippet example for Next() Method with rest of language versions. (dotnet#9759)
The example doesn't represent a description of it, which has the statement, `generate a specific number of random numbers requested by the user.`. In the current example, the number of random numbers is hard coded to 10 and doesn't read from user input.
1 parent ae3396c commit 394571c

File tree

1 file changed

+34
-23
lines changed
  • snippets/csharp/System/Random/Overview

1 file changed

+34
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
1-
using System;
1+
using System;
22

33
public class Example
44
{
5-
public static void Main()
6-
{
7-
// <Snippet5>
8-
Random rnd = new Random();
5+
public static void Main()
6+
{
7+
// <Snippet5>
8+
Console.Write("Number of random numbers to generate: ");
99

10-
Console.WriteLine("Generating 10 random numbers:");
10+
string? line = Console.ReadLine();
11+
Random rnd = new Random();
1112

12-
for (uint ctr = 1; ctr <= 10; ctr++)
13-
Console.WriteLine($"{rnd.Next(),15:N0}");
13+
if (!int.TryParse(line, out int numbers) || numbers <= 0)
14+
{
15+
numbers = 10;
16+
}
1417

15-
// The example displays output like the following:
16-
//
17-
// Generating 10 random numbers:
18-
// 1,733,189,596
19-
// 566,518,090
20-
// 1,166,108,546
21-
// 1,931,426,514
22-
// 1,532,939,448
23-
// 762,207,767
24-
// 815,074,920
25-
// 1,521,208,785
26-
// 1,950,436,671
27-
// 1,266,596,666
28-
// </Snippet5>
29-
}
18+
for (uint ctr = 1; ctr <= numbers; ctr++)
19+
Console.WriteLine($"{rnd.Next(),15:N0}");
20+
21+
// The example displays output like the following when asked to generate
22+
// 15 random numbers:
23+
// Number of random numbers to generate: 15
24+
// 367 920 603
25+
// 1 143 790 667
26+
// 1 360 963 275
27+
// 1 851 697 775
28+
// 248 956 796
29+
// 1 009 615 458
30+
// 1 617 743 155
31+
// 1 821 609 652
32+
// 1 661 761 949
33+
// 477 300 794
34+
// 288 418 129
35+
// 425 371 492
36+
// 1 558 147 880
37+
// 1 473 704 017
38+
// 777 507 489
39+
// </Snippet5>
40+
}
3041
}

0 commit comments

Comments
 (0)