Skip to content

Commit 18664fb

Browse files
albert-dubaronfel
andauthored
F# Snippets for System.Random (dotnet#7335)
* Add F# Snippets for System.Random * rename file * system.Random.Ctor snippets * system.Random.Next snippets * system.random.nextdouble snippet * system.Random.Sample snippet * double -> float F# floats are 64bit and equivalent to doubles changed for consistency * fix file name * minor adjustments * fixed snippet number * fixed float printing to be more consistent with .ToString() * Add modules and fsproj files * Apply suggestions from code review Co-authored-by: Chet Husk <[email protected]> * Update Random.xml Include information for F# * improve snippet formatting removed type annotations from array creation replaced another "double" with "float" normalized formatting * use string interpolation Co-authored-by: Chet Husk <[email protected]>
1 parent 97bfab3 commit 18664fb

36 files changed

+1574
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="ctor.fs" />
10+
<Compile Include="ctor1.fs" />
11+
<Compile Include="ctor4.fs" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
module Ctor
2+
3+
//<Snippet1>
4+
// Example of the Random class constructors and Random.NextDouble()
5+
// method.
6+
open System
7+
open System.Threading
8+
9+
// Generate random numbers from the specified Random object.
10+
let runIntNDoubleRandoms (randObj: Random) =
11+
// Generate the first six random integers.
12+
for _ = 1 to 6 do
13+
printf $" {randObj.Next(),10} "
14+
printfn ""
15+
16+
// Generate the first six random doubles.
17+
for _ = 1 to 6 do
18+
printf $" {randObj.NextDouble():F8} "
19+
printfn ""
20+
21+
let fixedSeedRandoms seed =
22+
printfn $"\nRandom numbers from a Random object with seed = %i{seed}:"
23+
let fixRand = Random seed
24+
25+
runIntNDoubleRandoms fixRand
26+
27+
let autoSeedRandoms () =
28+
// Wait to allow the timer to advance.
29+
Thread.Sleep 1
30+
31+
printfn "\nRandom numbers from a Random object with an auto-generated seed: "
32+
let autoRand = Random ()
33+
34+
runIntNDoubleRandoms autoRand
35+
36+
printfn
37+
"""This example of the Random class constructors and Random.NextDouble()
38+
generates the following output.
39+
Create Random objects, and then generate and display six integers and
40+
six doubles from each."""
41+
42+
fixedSeedRandoms 123
43+
fixedSeedRandoms 123
44+
45+
fixedSeedRandoms 456
46+
fixedSeedRandoms 456
47+
48+
autoSeedRandoms ()
49+
autoSeedRandoms ()
50+
autoSeedRandoms ()
51+
52+
(*
53+
This example of the Random class constructors and Random.NextDouble()
54+
generates an output similar to the following:
55+
56+
Create Random objects, and then generate and display six integers and
57+
six doubles from each.
58+
59+
Random numbers from a Random object with seed = 123:
60+
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
61+
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
62+
63+
Random numbers from a Random object with seed = 123:
64+
2114319875 1949518561 1596751841 1742987178 1586516133 103755708
65+
0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146
66+
67+
Random numbers from a Random object with seed = 456:
68+
2044805024 1323311594 1087799997 1907260840 179380355 120870348
69+
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
70+
71+
Random numbers from a Random object with seed = 456:
72+
2044805024 1323311594 1087799997 1907260840 179380355 120870348
73+
0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
74+
75+
Random numbers from a Random object with an auto-generated seed:
76+
380213349 127379247 1969091178 1983029819 1963098450 1648433124
77+
0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560
78+
79+
Random numbers from a Random object with an auto-generated seed:
80+
861793304 2133528783 1947358439 124230908 921262645 1087892791
81+
0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005
82+
83+
Random numbers from a Random object with an auto-generated seed:
84+
1343373259 1992194672 1925625700 412915644 2026910487 527352458
85+
0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451
86+
*)
87+
//</Snippet1>
88+
89+
// Code added to show how to initialize Random objects with the
90+
// same timer value that will produce unique random number sequences.
91+
let createEngineWithSameTimer () =
92+
// <Snippet3>
93+
let randomEngines =
94+
[| for i in 0 .. 3 -> Random(int (DateTime.Now.Ticks >>> i)) |]
95+
// </Snippet3>
96+
for i in randomEngines do
97+
printfn "%i" (i.Next())
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Ctor1
2+
3+
// <Snippet2>
4+
open System
5+
open System.Threading
6+
7+
let showRandomNumbers (rand: Random) =
8+
printfn ""
9+
let values = Array.zeroCreate 5
10+
rand.NextBytes values
11+
for value in values do
12+
printf "%5i" value
13+
printfn ""
14+
15+
let rand1 = Random()
16+
let rand2 = Random()
17+
Thread.Sleep 2000
18+
let rand3 = Random()
19+
20+
showRandomNumbers rand1
21+
showRandomNumbers rand2
22+
showRandomNumbers rand3
23+
24+
// The example displays an output similar to the following:
25+
// 28 35 133 224 58
26+
//
27+
// 28 35 133 224 58
28+
//
29+
// 32 222 43 251 49
30+
// </Snippet2>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Ctor4
2+
3+
// <Snippet4>
4+
open System
5+
open System.Threading
6+
7+
let showRandomNumbers (rand: Random) =
8+
printfn ""
9+
let values = Array.zeroCreate 5
10+
rand.NextBytes values
11+
for value in values do
12+
printf "%5i" value
13+
printfn ""
14+
15+
let rand1 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
16+
let rand2 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
17+
Thread.Sleep 20
18+
let rand3 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
19+
20+
showRandomNumbers rand1
21+
showRandomNumbers rand2
22+
showRandomNumbers rand3
23+
24+
// The example displays output similar to the following:
25+
// 145 214 177 134 173
26+
//
27+
// 145 214 177 134 173
28+
//
29+
// 126 185 175 249 157
30+
// </Snippet4>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Next2
2+
3+
open System
4+
5+
//<Snippet2>
6+
let rnd = Random()
7+
8+
printfn "\n20 random integers from -100 to 100:"
9+
for i = 1 to 20 do
10+
printf "%6i" (rnd.Next(-100,100))
11+
if i % 5 = 0 then printfn ""
12+
13+
printfn "\n20 random integers from 1000 to 10000:"
14+
for i = 1 to 20 do
15+
printf "%8i" (rnd.Next(1000,10001))
16+
if i % 5 = 0 then printfn ""
17+
18+
printfn "\n20 random integers from 1 to 10:"
19+
for i = 1 to 20 do
20+
printf "%6i" (rnd.Next(1,11))
21+
if i % 5 = 0 then printfn ""
22+
23+
// The example displays output similar to the following:
24+
// 20 random integers from -100 to 100:
25+
// 65 -95 -10 90 -35
26+
// -83 -16 -15 -19 41
27+
// -67 -93 40 12 62
28+
// -80 -95 67 -81 -21
29+
//
30+
// 20 random integers from 1000 to 10000:
31+
// 4857 9897 4405 6606 1277
32+
// 9238 9113 5151 8710 1187
33+
// 2728 9746 1719 3837 3736
34+
// 8191 6819 4923 2416 3028
35+
//
36+
// 20 random integers from 1 to 10:
37+
// 9 8 5 9 9
38+
// 9 1 2 3 8
39+
// 1 4 8 10 5
40+
// 9 7 9 10 5
41+
// </Snippet2>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="next.fs" />
10+
<Compile Include="next1.fs" />
11+
<Compile Include="Next2.fs" />
12+
<Compile Include="next3.fs" />
13+
<Compile Include="next4.fs" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
module Next
2+
3+
// Example of the Random.Next() methods.
4+
open System
5+
6+
//<Snippet1>
7+
let noBoundsRandoms seed =
8+
printfn "\nRandom object, seed = %i, no bounds:" seed
9+
let randObj = Random seed
10+
11+
// Generate six random integers from 0 to int.MaxValue.
12+
for _ = 1 to 6 do
13+
printf $"%11i{randObj.Next()} "
14+
printfn ""
15+
16+
// Generate random numbers with an upper bound specified.
17+
let upperBoundRandoms seed upper =
18+
printfn $"\nRandom object, seed = %i{seed}, upper bound = %i{upper}"
19+
let randObj = Random seed
20+
21+
// Generate six random integers from 0 to the upper bound.
22+
for _ = 1 to 6 do
23+
printf $"%11i{randObj.Next upper} "
24+
printfn ""
25+
26+
// Generate random numbers with both bounds specified.
27+
let bothBoundRandoms seed lower upper =
28+
printfn $"\nRandom object, seed = %i{seed}, lower = %i{lower}, upper = %i{upper}: "
29+
let randObj = Random seed
30+
31+
// Generate six random integers from the lower to upper bounds.
32+
for _ = 1 to 6 do
33+
printf $"%11i{randObj.Next(lower,upper)} "
34+
printfn ""
35+
36+
printfn "This example of the Random.Next() methods\ngenerates the following.\n"
37+
38+
printfn """Create Random objects all with the same seed and generate
39+
sequences of numbers with different bounds. Note the effect
40+
that the various combinations of bounds have on the sequences."""
41+
42+
noBoundsRandoms 234
43+
44+
upperBoundRandoms 234 Int32.MaxValue
45+
upperBoundRandoms 234 2000000000
46+
upperBoundRandoms 234 200000000
47+
48+
bothBoundRandoms 234 0 Int32.MaxValue
49+
bothBoundRandoms 234 Int32.MinValue Int32.MaxValue
50+
bothBoundRandoms 234 -2000000000 2000000000
51+
bothBoundRandoms 234 -200000000 200000000
52+
bothBoundRandoms 234 -2000 2000
53+
54+
(*
55+
This example of the Random.Next() methods
56+
generates the following output.
57+
58+
Create Random objects all with the same seed and generate
59+
sequences of numbers with different bounds. Note the effect
60+
that the various combinations of bounds have on the sequences.
61+
62+
Random object, seed = 234, no bounds:
63+
2091148258 1024955023 711273344 1081917183 1833298756 109460588
64+
65+
Random object, seed = 234, upper bound = 2147483647:
66+
2091148258 1024955023 711273344 1081917183 1833298756 109460588
67+
68+
Random object, seed = 234, upper bound = 2000000000:
69+
1947533580 954563751 662424922 1007613896 1707392518 101943116
70+
71+
Random object, seed = 234, upper bound = 200000000:
72+
194753358 95456375 66242492 100761389 170739251 10194311
73+
74+
Random object, seed = 234, lower = 0, upper = 2147483647:
75+
2091148258 1024955023 711273344 1081917183 1833298756 109460588
76+
77+
Random object, seed = 234, lower = -2147483648, upper = 2147483647:
78+
2034812868 -97573602 -724936960 16350718 1519113864 -1928562472
79+
80+
Random object, seed = 234, lower = -2000000000, upper = 2000000000:
81+
1895067160 -90872498 -675150156 15227793 1414785036 -1796113767
82+
83+
Random object, seed = 234, lower = -200000000, upper = 200000000:
84+
189506716 -9087250 -67515016 1522779 141478503 -179611377
85+
86+
Random object, seed = 234, lower = -2000, upper = 2000:
87+
1895 -91 -676 15 1414 -1797
88+
*)
89+
//</Snippet1>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Next1
2+
3+
open System
4+
5+
// <Snippet3>
6+
let rnd = Random()
7+
8+
let malePetNames =
9+
[| "Rufus"; "Bear"; "Dakota"; "Fido";
10+
"Vanya"; "Samuel"; "Koani"; "Volodya";
11+
"Prince"; "Yiska" |]
12+
let femalePetNames =
13+
[| "Maggie"; "Penny"; "Saya"; "Princess";
14+
"Abby"; "Laila"; "Sadie"; "Olivia";
15+
"Starlight"; "Talla" |]
16+
17+
// Generate random indexes for pet names.
18+
let mIndex = rnd.Next malePetNames.Length
19+
let fIndex = rnd.Next femalePetNames.Length
20+
21+
// Display the result.
22+
printfn "Suggested pet name of the day: "
23+
printfn " For a male: %s" malePetNames.[mIndex]
24+
printfn " For a female: %s" femalePetNames.[fIndex]
25+
26+
// The example displays output similar to the following:
27+
// Suggested pet name of the day:
28+
// For a male: Koani
29+
// For a female: Maggie
30+
// </Snippet3>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Next3
2+
3+
open System
4+
5+
// <Snippet5>
6+
let rnd = Random()
7+
8+
printfn "Generating 10 random numbers:"
9+
10+
for _ = 1 to 10 do
11+
printfn $"{rnd.Next(),15:N0}"
12+
13+
// The example displays output like the following:
14+
//
15+
// Generating 10 random numbers:
16+
// 1,733,189,596
17+
// 566,518,090
18+
// 1,166,108,546
19+
// 1,931,426,514
20+
// 1,532,939,448
21+
// 762,207,767
22+
// 815,074,920
23+
// 1,521,208,785
24+
// 1,950,436,671
25+
// 1,266,596,666
26+
// </Snippet5>

0 commit comments

Comments
 (0)