Skip to content

Add project file and modernize code #9770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions snippets/csharp/System/Random/Overview/Next2.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;

public class Example
public class Example9
{
public static void Main()
{
//<Snippet2>
Random rnd = new Random();
Random rnd = new();

Console.WriteLine("\n20 random integers from -100 to 100:");
for (int ctr = 1; ctr <= 20; ctr++)
Expand Down
104 changes: 54 additions & 50 deletions snippets/csharp/System/Random/Overview/Random1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,63 @@

public class Class1
{
public static void Main()
{
// <Snippet1>
byte[] bytes1 = new byte[100];
byte[] bytes2 = new byte[100];
Random rnd1 = new Random();
Random rnd2 = new Random();
public static void Main()
{
// <Snippet1>
byte[] bytes1 = new byte[100];
byte[] bytes2 = new byte[100];
Random rnd1 = new();
Random rnd2 = new();

rnd1.NextBytes(bytes1);
rnd2.NextBytes(bytes2);
rnd1.NextBytes(bytes1);
rnd2.NextBytes(bytes2);

Console.WriteLine("First Series:");
for (int ctr = bytes1.GetLowerBound(0);
ctr <= bytes1.GetUpperBound(0);
ctr++) {
Console.Write("{0, 5}", bytes1[ctr]);
if ((ctr + 1) % 10 == 0) Console.WriteLine();
}
Console.WriteLine("First Series:");
for (int ctr = bytes1.GetLowerBound(0);
ctr <= bytes1.GetUpperBound(0);
ctr++)
{
Console.Write($"{bytes1[ctr],5}");
if ((ctr + 1) % 10 == 0)
Console.WriteLine();
}

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Second Series:");
for (int ctr = bytes2.GetLowerBound(0);
ctr <= bytes2.GetUpperBound(0);
ctr++) {
Console.Write("{0, 5}", bytes2[ctr]);
if ((ctr + 1) % 10 == 0) Console.WriteLine();
}
Console.WriteLine("Second Series:");
for (int ctr = bytes2.GetLowerBound(0);
ctr <= bytes2.GetUpperBound(0);
ctr++)
{
Console.Write("{bytes2[ctr], 5}");
if ((ctr + 1) % 10 == 0)
Console.WriteLine();
}

// The example displays output like the following:
// First Series:
// 97 129 149 54 22 208 120 105 68 177
// 113 214 30 172 74 218 116 230 89 18
// 12 112 130 105 116 180 190 200 187 120
// 7 198 233 158 58 51 50 170 98 23
// 21 1 113 74 146 245 34 255 96 24
// 232 255 23 9 167 240 255 44 194 98
// 18 175 173 204 169 171 236 127 114 23
// 167 202 132 65 253 11 254 56 214 127
// 145 191 104 163 143 7 174 224 247 73
// 52 6 231 255 5 101 83 165 160 231
//
// Second Series:
// 97 129 149 54 22 208 120 105 68 177
// 113 214 30 172 74 218 116 230 89 18
// 12 112 130 105 116 180 190 200 187 120
// 7 198 233 158 58 51 50 170 98 23
// 21 1 113 74 146 245 34 255 96 24
// 232 255 23 9 167 240 255 44 194 98
// 18 175 173 204 169 171 236 127 114 23
// 167 202 132 65 253 11 254 56 214 127
// 145 191 104 163 143 7 174 224 247 73
// 52 6 231 255 5 101 83 165 160 231
// </Snippet1>
}
// The example displays output like the following:
// First Series:
// 97 129 149 54 22 208 120 105 68 177
// 113 214 30 172 74 218 116 230 89 18
// 12 112 130 105 116 180 190 200 187 120
// 7 198 233 158 58 51 50 170 98 23
// 21 1 113 74 146 245 34 255 96 24
// 232 255 23 9 167 240 255 44 194 98
// 18 175 173 204 169 171 236 127 114 23
// 167 202 132 65 253 11 254 56 214 127
// 145 191 104 163 143 7 174 224 247 73
// 52 6 231 255 5 101 83 165 160 231
//
// Second Series:
// 97 129 149 54 22 208 120 105 68 177
// 113 214 30 172 74 218 116 230 89 18
// 12 112 130 105 116 180 190 200 187 120
// 7 198 233 158 58 51 50 170 98 23
// 21 1 113 74 146 245 34 255 96 24
// 232 255 23 9 167 240 255 44 194 98
// 18 175 173 204 169 171 236 127 114 23
// 167 202 132 65 253 11 254 56 214 127
// 145 191 104 163 143 7 174 224 247 73
// 52 6 231 255 5 101 83 165 160 231
// </Snippet1>
}
}
4 changes: 2 additions & 2 deletions snippets/csharp/System/Random/Overview/Random2.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

public class Class1
public class Class2
{
public static void Main()
{
Expand All @@ -9,7 +9,7 @@ public static void Main()
var rand = new Random();

// Generate and display 5 random byte (integer) values.
var bytes = new byte[5];
byte[] bytes = new byte[5];
rand.NextBytes(bytes);
Console.WriteLine("Five random byte values:");
foreach (byte byteValue in bytes)
Expand Down
27 changes: 13 additions & 14 deletions snippets/csharp/System/Random/Overview/array1.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using System;

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

// The example displays output like the following:
// Today's city of the day: Honolulu
// </Snippet10>
}
// The example displays output like the following:
// Today's city of the day: Honolulu
// </Snippet10>
}
}
61 changes: 31 additions & 30 deletions snippets/csharp/System/Random/Overview/booleans1.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
// <Snippet8>
using System;

public class Example
public class Example2
{
public static void Main()
{
// Instantiate the Boolean generator.
BooleanGenerator boolGen = new BooleanGenerator();
int totalTrue = 0, totalFalse = 0;
public static void Main()
{
// Instantiate the Boolean generator.
BooleanGenerator boolGen = new();
int totalTrue = 0, totalFalse = 0;

// Generate 1,0000 random Booleans, and keep a running total.
for (int ctr = 0; ctr < 1000000; ctr++) {
bool value = boolGen.NextBoolean();
if (value)
totalTrue++;
else
totalFalse++;
}
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
totalTrue,
((double) totalTrue)/(totalTrue + totalFalse));
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
totalFalse,
((double) totalFalse)/(totalTrue + totalFalse));
}
// Generate 1,0000 random Booleans, and keep a running total.
for (int ctr = 0; ctr < 1000000; ctr++)
{
bool value = boolGen.NextBoolean();
if (value)
totalTrue++;
else
totalFalse++;
}
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
totalTrue,
((double)totalTrue) / (totalTrue + totalFalse));
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
totalFalse,
((double)totalFalse) / (totalTrue + totalFalse));
}
}

public class BooleanGenerator
{
Random rnd;
Random rnd;

public BooleanGenerator()
{
rnd = new Random();
}
public BooleanGenerator()
{
rnd = new Random();
}

public bool NextBoolean()
{
return rnd.Next(0, 2) == 1;
}
public bool NextBoolean()
{
return rnd.Next(0, 2) == 1;
}
}
// The example displays output like the following:
// Number of true values: 500,004 (50.000 %)
Expand Down
59 changes: 30 additions & 29 deletions snippets/csharp/System/Random/Overview/booleans2.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
using System;

public class Example
public class Example3
{
public static void Main()
{
// <Snippet20>
Random rnd = new Random();
public static void Main()
{
// <Snippet20>
Random rnd = new();

int totalTrue = 0, totalFalse = 0;
int totalTrue = 0, totalFalse = 0;

// Generate 1,000,000 random Booleans, and keep a running total.
for (int ctr = 0; ctr < 1000000; ctr++) {
bool value = NextBoolean();
if (value)
totalTrue++;
else
totalFalse++;
}
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
totalTrue,
((double) totalTrue)/(totalTrue + totalFalse));
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
totalFalse,
((double) totalFalse)/(totalTrue + totalFalse));
// Generate 1,000,000 random Booleans, and keep a running total.
for (int ctr = 0; ctr < 1000000; ctr++)
{
bool value = NextBoolean();
if (value)
totalTrue++;
else
totalFalse++;
}
Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
totalTrue,
((double)totalTrue) / (totalTrue + totalFalse));
Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
totalFalse,
((double)totalFalse) / (totalTrue + totalFalse));

bool NextBoolean()
{
return rnd.Next(0, 2) == 1;
}
bool NextBoolean()
{
return rnd.Next(0, 2) == 1;
}

// The example displays output like the following:
// Number of true values: 499,777 (49.978 %)
// Number of false values: 500,223 (50.022 %)
// </Snippet20>
}
// The example displays output like the following:
// Number of true values: 499,777 (49.978 %)
// Number of false values: 500,223 (50.022 %)
// </Snippet20>
}
}
52 changes: 27 additions & 25 deletions snippets/csharp/System/Random/Overview/bytes1.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
// <Snippet9>
using System;

public class Example
public class Example4
{
public static void Main()
{
Random2 rnd = new Random2();
Byte[] bytes = new Byte[10000];
int[] total = new int[101];
rnd.NextBytes(bytes, 0, 101);
public static void Main()
{
Random2 rnd = new();
byte[] bytes = new byte[10000];
int[] total = new int[101];
rnd.NextBytes(bytes, 0, 101);

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

// Display the results.
for (int ctr = 0; ctr < total.Length; ctr++) {
Console.Write("{0,3}: {1,-3} ", ctr, total[ctr]);
if ((ctr + 1) % 5 == 0) Console.WriteLine();
}
}
// Display the results.
for (int ctr = 0; ctr < total.Length; ctr++)
{
Console.Write($"{ctr,3}: {total[ctr],-3}");
if ((ctr + 1) % 5 == 0) Console.WriteLine();
}
}
}

public class Random2 : Random
{
public Random2() : base()
{}
public Random2() : base()
{ }

public Random2(int seed) : base(seed)
{}
public Random2(int seed) : base(seed)
{ }

public void NextBytes(byte[] bytes, byte minValue, byte maxValue)
{
for (int ctr = bytes.GetLowerBound(0); ctr <= bytes.GetUpperBound(0); ctr++)
bytes[ctr] = (byte) Next(minValue, maxValue);
}
public void NextBytes(byte[] bytes, byte minValue, byte maxValue)
{
for (int ctr = bytes.GetLowerBound(0); ctr <= bytes.GetUpperBound(0); ctr++)
bytes[ctr] = (byte)Next(minValue, maxValue);
}
}

// The example displays output like the following:
// 0: 115 1: 119 2: 92 3: 98 4: 92
// 5: 102 6: 103 7: 84 8: 93 9: 116
Expand Down
Loading
Loading