1
- // Example of the Random class constructors and Random.NextDouble( )
1
+ //<Snippet1>
2
+ // Example of the Random class constructors and Random.NextDouble( )
2
3
// method.
3
4
using System ;
4
5
using System . Threading ;
5
6
6
7
public class RandomObjectDemo
7
8
{
9
+ // Generate random numbers from the specified Random object.
10
+ static void RunIntNDoubleRandoms ( Random randObj )
11
+ {
12
+ // Generate the first six random integers.
13
+ for ( int j = 0 ; j < 6 ; j ++ )
14
+ Console . Write ( " {0,10} " , randObj . Next ( ) ) ;
15
+ Console . WriteLine ( ) ;
16
+
17
+ // Generate the first six random doubles.
18
+ for ( int j = 0 ; j < 6 ; j ++ )
19
+ Console . Write ( " {0:F8} " , randObj . NextDouble ( ) ) ;
20
+ Console . WriteLine ( ) ;
21
+ }
22
+
23
+ // Create a Random object with the specified seed.
24
+ static void FixedSeedRandoms ( int seed )
25
+ {
26
+ Console . WriteLine (
27
+ "\n Random numbers from a Random object with " +
28
+ "seed = {0}:" , seed ) ;
29
+ Random fixRand = new Random ( seed ) ;
30
+
31
+ RunIntNDoubleRandoms ( fixRand ) ;
32
+ }
33
+
34
+ // Create a random object with a timer-generated seed.
35
+ static void AutoSeedRandoms ( )
36
+ {
37
+ // Wait to allow the timer to advance.
38
+ Thread . Sleep ( 1 ) ;
39
+
40
+ Console . WriteLine (
41
+ "\n Random numbers from a Random object " +
42
+ "with an auto-generated seed:" ) ;
43
+ Random autoRand = new Random ( ) ;
44
+
45
+ RunIntNDoubleRandoms ( autoRand ) ;
46
+ }
47
+
8
48
static void Main ( )
9
49
{
10
- //<Snippet1>
11
50
Console . WriteLine (
12
51
"This example of the Random class constructors and " +
13
52
"Random.NextDouble( ) \n " +
@@ -25,85 +64,45 @@ static void Main( )
25
64
AutoSeedRandoms ( ) ;
26
65
AutoSeedRandoms ( ) ;
27
66
AutoSeedRandoms ( ) ;
28
-
29
- // Generate random numbers from the specified Random object.
30
- void RunIntNDoubleRandoms ( Random randObj )
31
- {
32
- // Generate the first six random integers.
33
- for ( int j = 0 ; j < 6 ; j ++ )
34
- Console . Write ( " {0,10} " , randObj . Next ( ) ) ;
35
- Console . WriteLine ( ) ;
36
-
37
- // Generate the first six random doubles.
38
- for ( int j = 0 ; j < 6 ; j ++ )
39
- Console . Write ( " {0:F8} " , randObj . NextDouble ( ) ) ;
40
- Console . WriteLine ( ) ;
41
- }
42
-
43
- // Create a Random object with the specified seed.
44
- void FixedSeedRandoms ( int seed )
45
- {
46
- Console . WriteLine (
47
- "\n Random numbers from a Random object with " +
48
- "seed = {0}:" , seed ) ;
49
- Random fixRand = new Random ( seed ) ;
50
-
51
- RunIntNDoubleRandoms ( fixRand ) ;
52
- }
53
-
54
- // Create a random object with a timer-generated seed.
55
- void AutoSeedRandoms ( )
56
- {
57
- // Wait to allow the timer to advance.
58
- System . Threading . Thread . Sleep ( 1 ) ;
59
-
60
- Console . WriteLine (
61
- "\n Random numbers from a Random object " +
62
- "with an auto-generated seed:" ) ;
63
- Random autoRand = new Random ( ) ;
64
-
65
- RunIntNDoubleRandoms ( autoRand ) ;
66
- }
67
-
68
- /*
69
- This example of the Random class constructors and Random.NextDouble( )
70
- generates the following output.
71
-
72
- Create Random objects, and then generate and display six integers and
73
- six doubles from each.
74
-
75
- Random numbers from a Random object with seed = 123:
76
- 2114319875 1949518561 1596751841 1742987178 1586516133 103755708
77
- 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
78
-
79
- Random numbers from a Random object with seed = 123:
80
- 2114319875 1949518561 1596751841 1742987178 1586516133 103755708
81
- 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
82
-
83
- Random numbers from a Random object with seed = 456:
84
- 2044805024 1323311594 1087799997 1907260840 179380355 120870348
85
- 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
86
-
87
- Random numbers from a Random object with seed = 456:
88
- 2044805024 1323311594 1087799997 1907260840 179380355 120870348
89
- 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
90
-
91
- Random numbers from a Random object with an auto-generated seed:
92
- 380213349 127379247 1969091178 1983029819 1963098450 1648433124
93
- 0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560
94
-
95
- Random numbers from a Random object with an auto-generated seed:
96
- 861793304 2133528783 1947358439 124230908 921262645 1087892791
97
- 0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005
98
-
99
- Random numbers from a Random object with an auto-generated seed:
100
- 1343373259 1992194672 1925625700 412915644 2026910487 527352458
101
- 0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451
102
- */
103
- //</Snippet1>
104
67
}
105
68
}
106
69
70
+ /*
71
+ This example of the Random class constructors and Random.NextDouble( )
72
+ generates the following output.
73
+
74
+ Create Random objects, and then generate and display six integers and
75
+ six doubles from each.
76
+
77
+ Random numbers from a Random object with seed = 123:
78
+ 2114319875 1949518561 1596751841 1742987178 1586516133 103755708
79
+ 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
80
+
81
+ Random numbers from a Random object with seed = 123:
82
+ 2114319875 1949518561 1596751841 1742987178 1586516133 103755708
83
+ 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
84
+
85
+ Random numbers from a Random object with seed = 456:
86
+ 2044805024 1323311594 1087799997 1907260840 179380355 120870348
87
+ 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
88
+
89
+ Random numbers from a Random object with seed = 456:
90
+ 2044805024 1323311594 1087799997 1907260840 179380355 120870348
91
+ 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
92
+
93
+ Random numbers from a Random object with an auto-generated seed:
94
+ 380213349 127379247 1969091178 1983029819 1963098450 1648433124
95
+ 0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560
96
+
97
+ Random numbers from a Random object with an auto-generated seed:
98
+ 861793304 2133528783 1947358439 124230908 921262645 1087892791
99
+ 0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005
100
+
101
+ Random numbers from a Random object with an auto-generated seed:
102
+ 1343373259 1992194672 1925625700 412915644 2026910487 527352458
103
+ 0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451
104
+ */
105
+ //</Snippet1>
107
106
108
107
// Code added to show how to initialize Random objects with the
109
108
// same timer value that will produce unique random number sequences.
0 commit comments