Skip to content

Commit ba5b6bb

Browse files
authored
Add project file and modernize code (#9770)
1 parent a31d808 commit ba5b6bb

25 files changed

+754
-689
lines changed

snippets/csharp/System/Random/Overview/Next2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22

3-
public class Example
3+
public class Example9
44
{
55
public static void Main()
66
{
77
//<Snippet2>
8-
Random rnd = new Random();
8+
Random rnd = new();
99

1010
Console.WriteLine("\n20 random integers from -100 to 100:");
1111
for (int ctr = 1; ctr <= 20; ctr++)

snippets/csharp/System/Random/Overview/Random1.cs

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,63 @@
22

33
public class Class1
44
{
5-
public static void Main()
6-
{
7-
// <Snippet1>
8-
byte[] bytes1 = new byte[100];
9-
byte[] bytes2 = new byte[100];
10-
Random rnd1 = new Random();
11-
Random rnd2 = new Random();
5+
public static void Main()
6+
{
7+
// <Snippet1>
8+
byte[] bytes1 = new byte[100];
9+
byte[] bytes2 = new byte[100];
10+
Random rnd1 = new();
11+
Random rnd2 = new();
1212

13-
rnd1.NextBytes(bytes1);
14-
rnd2.NextBytes(bytes2);
13+
rnd1.NextBytes(bytes1);
14+
rnd2.NextBytes(bytes2);
1515

16-
Console.WriteLine("First Series:");
17-
for (int ctr = bytes1.GetLowerBound(0);
18-
ctr <= bytes1.GetUpperBound(0);
19-
ctr++) {
20-
Console.Write("{0, 5}", bytes1[ctr]);
21-
if ((ctr + 1) % 10 == 0) Console.WriteLine();
22-
}
16+
Console.WriteLine("First Series:");
17+
for (int ctr = bytes1.GetLowerBound(0);
18+
ctr <= bytes1.GetUpperBound(0);
19+
ctr++)
20+
{
21+
Console.Write($"{bytes1[ctr],5}");
22+
if ((ctr + 1) % 10 == 0)
23+
Console.WriteLine();
24+
}
2325

24-
Console.WriteLine();
26+
Console.WriteLine();
2527

26-
Console.WriteLine("Second Series:");
27-
for (int ctr = bytes2.GetLowerBound(0);
28-
ctr <= bytes2.GetUpperBound(0);
29-
ctr++) {
30-
Console.Write("{0, 5}", bytes2[ctr]);
31-
if ((ctr + 1) % 10 == 0) Console.WriteLine();
32-
}
28+
Console.WriteLine("Second Series:");
29+
for (int ctr = bytes2.GetLowerBound(0);
30+
ctr <= bytes2.GetUpperBound(0);
31+
ctr++)
32+
{
33+
Console.Write("{bytes2[ctr], 5}");
34+
if ((ctr + 1) % 10 == 0)
35+
Console.WriteLine();
36+
}
3337

34-
// The example displays output like the following:
35-
// First Series:
36-
// 97 129 149 54 22 208 120 105 68 177
37-
// 113 214 30 172 74 218 116 230 89 18
38-
// 12 112 130 105 116 180 190 200 187 120
39-
// 7 198 233 158 58 51 50 170 98 23
40-
// 21 1 113 74 146 245 34 255 96 24
41-
// 232 255 23 9 167 240 255 44 194 98
42-
// 18 175 173 204 169 171 236 127 114 23
43-
// 167 202 132 65 253 11 254 56 214 127
44-
// 145 191 104 163 143 7 174 224 247 73
45-
// 52 6 231 255 5 101 83 165 160 231
46-
//
47-
// Second Series:
48-
// 97 129 149 54 22 208 120 105 68 177
49-
// 113 214 30 172 74 218 116 230 89 18
50-
// 12 112 130 105 116 180 190 200 187 120
51-
// 7 198 233 158 58 51 50 170 98 23
52-
// 21 1 113 74 146 245 34 255 96 24
53-
// 232 255 23 9 167 240 255 44 194 98
54-
// 18 175 173 204 169 171 236 127 114 23
55-
// 167 202 132 65 253 11 254 56 214 127
56-
// 145 191 104 163 143 7 174 224 247 73
57-
// 52 6 231 255 5 101 83 165 160 231
58-
// </Snippet1>
59-
}
38+
// The example displays output like the following:
39+
// First Series:
40+
// 97 129 149 54 22 208 120 105 68 177
41+
// 113 214 30 172 74 218 116 230 89 18
42+
// 12 112 130 105 116 180 190 200 187 120
43+
// 7 198 233 158 58 51 50 170 98 23
44+
// 21 1 113 74 146 245 34 255 96 24
45+
// 232 255 23 9 167 240 255 44 194 98
46+
// 18 175 173 204 169 171 236 127 114 23
47+
// 167 202 132 65 253 11 254 56 214 127
48+
// 145 191 104 163 143 7 174 224 247 73
49+
// 52 6 231 255 5 101 83 165 160 231
50+
//
51+
// Second Series:
52+
// 97 129 149 54 22 208 120 105 68 177
53+
// 113 214 30 172 74 218 116 230 89 18
54+
// 12 112 130 105 116 180 190 200 187 120
55+
// 7 198 233 158 58 51 50 170 98 23
56+
// 21 1 113 74 146 245 34 255 96 24
57+
// 232 255 23 9 167 240 255 44 194 98
58+
// 18 175 173 204 169 171 236 127 114 23
59+
// 167 202 132 65 253 11 254 56 214 127
60+
// 145 191 104 163 143 7 174 224 247 73
61+
// 52 6 231 255 5 101 83 165 160 231
62+
// </Snippet1>
63+
}
6064
}

snippets/csharp/System/Random/Overview/Random2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
public class Class1
3+
public class Class2
44
{
55
public static void Main()
66
{
@@ -9,7 +9,7 @@ public static void Main()
99
var rand = new Random();
1010

1111
// Generate and display 5 random byte (integer) values.
12-
var bytes = new byte[5];
12+
byte[] bytes = new byte[5];
1313
rand.NextBytes(bytes);
1414
Console.WriteLine("Five random byte values:");
1515
foreach (byte byteValue in bytes)
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System;
22

3-
public class Example
3+
public class Example1
44
{
5-
public static void Main()
6-
{
7-
// <Snippet10>
8-
String[] cities = { "Atlanta", "Boston", "Chicago", "Detroit",
5+
public static void Main()
6+
{
7+
// <Snippet10>
8+
string[] cities = [ "Atlanta", "Boston", "Chicago", "Detroit",
99
"Fort Wayne", "Greensboro", "Honolulu", "Indianapolis",
1010
"Jersey City", "Kansas City", "Los Angeles",
1111
"Milwaukee", "New York", "Omaha", "Philadelphia",
12-
"Raleigh", "San Francisco", "Tulsa", "Washington" };
13-
Random rnd = new Random();
14-
int index = rnd.Next(0, cities.Length);
15-
Console.WriteLine("Today's city of the day: {0}",
16-
cities[index]);
12+
"Raleigh", "San Francisco", "Tulsa", "Washington" ];
13+
Random rnd = new();
14+
int index = rnd.Next(0, cities.Length);
15+
Console.WriteLine($"Today's city of the day: {cities[index]}");
1716

18-
// The example displays output like the following:
19-
// Today's city of the day: Honolulu
20-
// </Snippet10>
21-
}
17+
// The example displays output like the following:
18+
// Today's city of the day: Honolulu
19+
// </Snippet10>
20+
}
2221
}

snippets/csharp/System/Random/Overview/booleans1.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
// <Snippet8>
22
using System;
33

4-
public class Example
4+
public class Example2
55
{
6-
public static void Main()
7-
{
8-
// Instantiate the Boolean generator.
9-
BooleanGenerator boolGen = new BooleanGenerator();
10-
int totalTrue = 0, totalFalse = 0;
6+
public static void Main()
7+
{
8+
// Instantiate the Boolean generator.
9+
BooleanGenerator boolGen = new();
10+
int totalTrue = 0, totalFalse = 0;
1111

12-
// Generate 1,0000 random Booleans, and keep a running total.
13-
for (int ctr = 0; ctr < 1000000; ctr++) {
14-
bool value = boolGen.NextBoolean();
15-
if (value)
16-
totalTrue++;
17-
else
18-
totalFalse++;
19-
}
20-
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
21-
totalTrue,
22-
((double) totalTrue)/(totalTrue + totalFalse));
23-
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
24-
totalFalse,
25-
((double) totalFalse)/(totalTrue + totalFalse));
26-
}
12+
// Generate 1,0000 random Booleans, and keep a running total.
13+
for (int ctr = 0; ctr < 1000000; ctr++)
14+
{
15+
bool value = boolGen.NextBoolean();
16+
if (value)
17+
totalTrue++;
18+
else
19+
totalFalse++;
20+
}
21+
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
22+
totalTrue,
23+
((double)totalTrue) / (totalTrue + totalFalse));
24+
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
25+
totalFalse,
26+
((double)totalFalse) / (totalTrue + totalFalse));
27+
}
2728
}
2829

2930
public class BooleanGenerator
3031
{
31-
Random rnd;
32+
Random rnd;
3233

33-
public BooleanGenerator()
34-
{
35-
rnd = new Random();
36-
}
34+
public BooleanGenerator()
35+
{
36+
rnd = new Random();
37+
}
3738

38-
public bool NextBoolean()
39-
{
40-
return rnd.Next(0, 2) == 1;
41-
}
39+
public bool NextBoolean()
40+
{
41+
return rnd.Next(0, 2) == 1;
42+
}
4243
}
4344
// The example displays output like the following:
4445
// Number of true values: 500,004 (50.000 %)
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
using System;
22

3-
public class Example
3+
public class Example3
44
{
5-
public static void Main()
6-
{
7-
// <Snippet20>
8-
Random rnd = new Random();
5+
public static void Main()
6+
{
7+
// <Snippet20>
8+
Random rnd = new();
99

10-
int totalTrue = 0, totalFalse = 0;
10+
int totalTrue = 0, totalFalse = 0;
1111

12-
// Generate 1,000,000 random Booleans, and keep a running total.
13-
for (int ctr = 0; ctr < 1000000; ctr++) {
14-
bool value = NextBoolean();
15-
if (value)
16-
totalTrue++;
17-
else
18-
totalFalse++;
19-
}
20-
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
21-
totalTrue,
22-
((double) totalTrue)/(totalTrue + totalFalse));
23-
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
24-
totalFalse,
25-
((double) totalFalse)/(totalTrue + totalFalse));
12+
// Generate 1,000,000 random Booleans, and keep a running total.
13+
for (int ctr = 0; ctr < 1000000; ctr++)
14+
{
15+
bool value = NextBoolean();
16+
if (value)
17+
totalTrue++;
18+
else
19+
totalFalse++;
20+
}
21+
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
22+
totalTrue,
23+
((double)totalTrue) / (totalTrue + totalFalse));
24+
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
25+
totalFalse,
26+
((double)totalFalse) / (totalTrue + totalFalse));
2627

27-
bool NextBoolean()
28-
{
29-
return rnd.Next(0, 2) == 1;
30-
}
28+
bool NextBoolean()
29+
{
30+
return rnd.Next(0, 2) == 1;
31+
}
3132

32-
// The example displays output like the following:
33-
// Number of true values: 499,777 (49.978 %)
34-
// Number of false values: 500,223 (50.022 %)
35-
// </Snippet20>
36-
}
33+
// The example displays output like the following:
34+
// Number of true values: 499,777 (49.978 %)
35+
// Number of false values: 500,223 (50.022 %)
36+
// </Snippet20>
37+
}
3738
}

snippets/csharp/System/Random/Overview/bytes1.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
// <Snippet9>
22
using System;
33

4-
public class Example
4+
public class Example4
55
{
6-
public static void Main()
7-
{
8-
Random2 rnd = new Random2();
9-
Byte[] bytes = new Byte[10000];
10-
int[] total = new int[101];
11-
rnd.NextBytes(bytes, 0, 101);
6+
public static void Main()
7+
{
8+
Random2 rnd = new();
9+
byte[] bytes = new byte[10000];
10+
int[] total = new int[101];
11+
rnd.NextBytes(bytes, 0, 101);
1212

13-
// Calculate how many of each value we have.
14-
foreach (var value in bytes)
15-
total[value]++;
13+
// Calculate how many of each value we have.
14+
foreach (byte value in bytes)
15+
total[value]++;
1616

17-
// Display the results.
18-
for (int ctr = 0; ctr < total.Length; ctr++) {
19-
Console.Write("{0,3}: {1,-3} ", ctr, total[ctr]);
20-
if ((ctr + 1) % 5 == 0) Console.WriteLine();
21-
}
22-
}
17+
// Display the results.
18+
for (int ctr = 0; ctr < total.Length; ctr++)
19+
{
20+
Console.Write($"{ctr,3}: {total[ctr],-3}");
21+
if ((ctr + 1) % 5 == 0) Console.WriteLine();
22+
}
23+
}
2324
}
2425

2526
public class Random2 : Random
2627
{
27-
public Random2() : base()
28-
{}
28+
public Random2() : base()
29+
{ }
2930

30-
public Random2(int seed) : base(seed)
31-
{}
31+
public Random2(int seed) : base(seed)
32+
{ }
3233

33-
public void NextBytes(byte[] bytes, byte minValue, byte maxValue)
34-
{
35-
for (int ctr = bytes.GetLowerBound(0); ctr <= bytes.GetUpperBound(0); ctr++)
36-
bytes[ctr] = (byte) Next(minValue, maxValue);
37-
}
34+
public void NextBytes(byte[] bytes, byte minValue, byte maxValue)
35+
{
36+
for (int ctr = bytes.GetLowerBound(0); ctr <= bytes.GetUpperBound(0); ctr++)
37+
bytes[ctr] = (byte)Next(minValue, maxValue);
38+
}
3839
}
40+
3941
// The example displays output like the following:
4042
// 0: 115 1: 119 2: 92 3: 98 4: 92
4143
// 5: 102 6: 103 7: 84 8: 93 9: 116

0 commit comments

Comments
 (0)