Skip to content
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
155 changes: 77 additions & 78 deletions snippets/csharp/VS_Snippets_CLR_System/system.Random.Ctor/CS/ctor.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
// Example of the Random class constructors and Random.NextDouble( )
//<Snippet1>
// Example of the Random class constructors and Random.NextDouble( )
// method.
using System;
using System.Threading;

public class RandomObjectDemo
{
// Generate random numbers from the specified Random object.
static void RunIntNDoubleRandoms( Random randObj )
{
// Generate the first six random integers.
for( int j = 0; j < 6; j++ )
Console.Write( " {0,10} ", randObj.Next( ) );
Console.WriteLine( );

// Generate the first six random doubles.
for( int j = 0; j < 6; j++ )
Console.Write( " {0:F8} ", randObj.NextDouble( ) );
Console.WriteLine( );
}

// Create a Random object with the specified seed.
static void FixedSeedRandoms( int seed )
{
Console.WriteLine(
"\nRandom numbers from a Random object with " +
"seed = {0}:", seed );
Random fixRand = new Random( seed );

RunIntNDoubleRandoms( fixRand );
}

// Create a random object with a timer-generated seed.
static void AutoSeedRandoms( )
{
// Wait to allow the timer to advance.
Thread.Sleep( 1 );

Console.WriteLine(
"\nRandom numbers from a Random object " +
"with an auto-generated seed:" );
Random autoRand = new Random( );

RunIntNDoubleRandoms( autoRand );
}

static void Main( )
{
//<Snippet1>
Console.WriteLine(
"This example of the Random class constructors and " +
"Random.NextDouble( ) \n" +
Expand All @@ -25,85 +64,45 @@ static void Main( )
AutoSeedRandoms( );
AutoSeedRandoms( );
AutoSeedRandoms( );

// Generate random numbers from the specified Random object.
void RunIntNDoubleRandoms( Random randObj )
{
// Generate the first six random integers.
for( int j = 0; j < 6; j++ )
Console.Write( " {0,10} ", randObj.Next( ) );
Console.WriteLine( );

// Generate the first six random doubles.
for( int j = 0; j < 6; j++ )
Console.Write( " {0:F8} ", randObj.NextDouble( ) );
Console.WriteLine( );
}

// Create a Random object with the specified seed.
void FixedSeedRandoms( int seed )
{
Console.WriteLine(
"\nRandom numbers from a Random object with " +
"seed = {0}:", seed );
Random fixRand = new Random( seed );

RunIntNDoubleRandoms( fixRand );
}

// Create a random object with a timer-generated seed.
void AutoSeedRandoms( )
{
// Wait to allow the timer to advance.
System.Threading.Thread.Sleep( 1 );

Console.WriteLine(
"\nRandom numbers from a Random object " +
"with an auto-generated seed:" );
Random autoRand = new Random( );

RunIntNDoubleRandoms( autoRand );
}

/*
This example of the Random class constructors and Random.NextDouble( )
generates the following output.

Create Random objects, and then generate and display six integers and
six doubles from each.

Random numbers from a Random object with seed = 123:
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146

Random numbers from a Random object with seed = 123:
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146

Random numbers from a Random object with seed = 456:
2044805024 1323311594 1087799997 1907260840 179380355 120870348
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170

Random numbers from a Random object with seed = 456:
2044805024 1323311594 1087799997 1907260840 179380355 120870348
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170

Random numbers from a Random object with an auto-generated seed:
380213349 127379247 1969091178 1983029819 1963098450 1648433124
0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560

Random numbers from a Random object with an auto-generated seed:
861793304 2133528783 1947358439 124230908 921262645 1087892791
0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005

Random numbers from a Random object with an auto-generated seed:
1343373259 1992194672 1925625700 412915644 2026910487 527352458
0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451
*/
//</Snippet1>
}
}

/*
This example of the Random class constructors and Random.NextDouble( )
generates the following output.

Create Random objects, and then generate and display six integers and
six doubles from each.

Random numbers from a Random object with seed = 123:
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146

Random numbers from a Random object with seed = 123:
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146

Random numbers from a Random object with seed = 456:
2044805024 1323311594 1087799997 1907260840 179380355 120870348
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170

Random numbers from a Random object with seed = 456:
2044805024 1323311594 1087799997 1907260840 179380355 120870348
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170

Random numbers from a Random object with an auto-generated seed:
380213349 127379247 1969091178 1983029819 1963098450 1648433124
0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560

Random numbers from a Random object with an auto-generated seed:
861793304 2133528783 1947358439 124230908 921262645 1087892791
0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005

Random numbers from a Random object with an auto-generated seed:
1343373259 1992194672 1925625700 412915644 2026910487 527352458
0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451
*/
//</Snippet1>

// Code added to show how to initialize Random objects with the
// same timer value that will produce unique random number sequences.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using System;
// <Snippet2>
using System;
using System.Threading;

public class RandomNumbers
{
public static void Main()
{
// <Snippet2>
Random rand1 = new Random();
Random rand2 = new Random();
System.Threading.Thread.Sleep(2000);
Thread.Sleep(2000);
Random rand3 = new Random();
ShowRandomNumbers(rand1);
ShowRandomNumbers(rand2);
ShowRandomNumbers(rand3);
}

void ShowRandomNumbers(Random rand)
{
Console.WriteLine();
byte[] values = new byte[5];
rand.NextBytes(values);
foreach (byte value in values)
Console.Write("{0, 5}", value);
Console.WriteLine();
}

// The example displays the following output to the console:
// 28 35 133 224 58
//
// 28 35 133 224 58
//
// 32 222 43 251 49
// </Snippet2>
private static void ShowRandomNumbers(Random rand)
{
Console.WriteLine();
byte[] values = new byte[5];
rand.NextBytes(values);
foreach (byte value in values)
Console.Write("{0, 5}", value);
Console.WriteLine();
}
}
// The example displays the following output to the console:
// 28 35 133 224 58
//
// 28 35 133 224 58
//
// 32 222 43 251 49
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
using System;
// <Snippet4>
using System;
using System.Threading;

public class Example
{
public static void Main()
{
// <Snippet4>
Random rand1 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
Random rand2 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
System.Threading.Thread.Sleep(20);
Thread.Sleep(20);
Random rand3 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
ShowRandomNumbers(rand1);
ShowRandomNumbers(rand2);
ShowRandomNumbers(rand3);
}

void ShowRandomNumbers(Random rand)
{
Console.WriteLine();
byte[] values = new byte[4];
rand.NextBytes(values);
foreach (var value in values)
Console.Write("{0, 5}", value);

Console.WriteLine();
}
private static void ShowRandomNumbers(Random rand)
{
Console.WriteLine();
byte[] values = new byte[4];
rand.NextBytes(values);
foreach (var value in values)
Console.Write("{0, 5}", value);

// The example displays output similar to the following:
// 145 214 177 134 173
//
// 145 214 177 134 173
//
// 126 185 175 249 157
// </Snippet4>
Console.WriteLine();
}
}
// The example displays output similar to the following:
// 145 214 177 134 173
//
// 145 214 177 134 173
//
// 126 185 175 249 157
// </Snippet4>
61 changes: 30 additions & 31 deletions snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/unique.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
// <Snippet13>
using System;
using System.Threading;

public class Example
{
public static void Main()
{
// <Snippet13>
Console.WriteLine("Instantiating two random number generators...");
Random rnd1 = new Random();
System.Threading.Thread.Sleep(2000);
Thread.Sleep(2000);
Random rnd2 = new Random();

Console.WriteLine("\nThe first random number generator:");
Expand All @@ -18,34 +18,33 @@ public static void Main()
Console.WriteLine("\nThe second random number generator:");
for (int ctr = 1; ctr <= 10; ctr++)
Console.WriteLine(" {0}", rnd2.Next());

// The example displays output like the following:
// Instantiating two random number generators...
//
// The first random number generator:
// 643164361
// 1606571630
// 1725607587
// 2138048432
// 496874898
// 1969147632
// 2034533749
// 1840964542
// 412380298
// 47518930
//
// The second random number generator:
// 1251659083
// 1514185439
// 1465798544
// 517841554
// 1821920222
// 195154223
// 1538948391
// 1548375095
// 546062716
// 897797880
// </Snippet13>
}
}
// The example displays output like the following:
// Instantiating two random number generators...
//
// The first random number generator:
// 643164361
// 1606571630
// 1725607587
// 2138048432
// 496874898
// 1969147632
// 2034533749
// 1840964542
// 412380298
// 47518930
//
// The second random number generator:
// 1251659083
// 1514185439
// 1465798544
// 517841554
// 1821920222
// 195154223
// 1538948391
// 1548375095
// 546062716
// 897797880
// </Snippet13>