From 51893c0349c3a5e380dde44d4f1e4196f0a465a4 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 17:58:35 -0700
Subject: [PATCH 01/14] Add F# Snippets for System.Random
---
.../system.Random/fs/Random1.fs | 48 ++++++
.../system.Random/fs/Random2.fs | 57 +++++++
.../system.Random/fs/array1.fs | 19 +++
.../system.Random/fs/booleans1.fs | 26 ++++
.../system.Random/fs/booleans2.fs | 24 +++
.../system.Random/fs/bytes.fs | 47 ++++++
.../system.Random/fs/doublerange1.fs | 35 +++++
.../system.Random/fs/doublerange2.fs | 20 +++
.../system.Random/fs/doublerange3.fs | 29 ++++
.../system.Random/fs/long1.fs | 35 +++++
.../system.Random/fs/nextbytes1.fs | 15 ++
.../system.Random/fs/nextdoubleex1.fs | 14 ++
.../system.Random/fs/nextex1.fs | 10 ++
.../system.Random/fs/range1.fs | 12 ++
.../system.Random/fs/range2.fs | 16 ++
.../system.Random/fs/same1.fs | 78 ++++++++++
.../system.Random/fs/threadsafeex1.fs | 141 ++++++++++++++++++
.../system.Random/fs/threadsafeex2.fs | 138 +++++++++++++++++
.../system.Random/fs/unique.fs | 44 ++++++
.../system.Random/fs/uniquearray1.fs | 76 ++++++++++
xml/System/Random.xml | 20 +++
21 files changed, 904 insertions(+)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
new file mode 100644
index 00000000000..ca391e4b79d
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
@@ -0,0 +1,48 @@
+open System
+
+//
+let bytes1 : byte [] = Array.zeroCreate 100
+let bytes2 : byte [] = Array.zeroCreate 100
+let rnd1 = Random()
+let rnd2 = Random()
+
+rnd1.NextBytes bytes1
+rnd2.NextBytes bytes2
+
+printfn "First Series"
+for i = bytes1.GetLowerBound 0 to bytes1.GetUpperBound 0 do
+ printf "%5i" bytes1.[i]
+ if (i + 1) % 10 = 0 then printfn ""
+
+printfn ""
+
+printfn "Second Series"
+for i = bytes2.GetLowerBound 0 to bytes2.GetUpperBound 0 do
+ printf "%5i" bytes2.[i]
+ if (i + 1) % 10 = 0 then printfn ""
+
+// 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
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
new file mode 100644
index 00000000000..e65c6c6f917
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
@@ -0,0 +1,57 @@
+open System
+
+//
+// Instantiate random number generator using system-supplied value as seed.
+let rand = Random()
+
+// Generate and display 5 random byte (integer) values.
+let bytes : byte [] = Array.zeroCreate 5
+rand.NextBytes bytes
+printfn "Five random byte values:"
+for byte in bytes do
+ printf "%5i" byte
+printfn ""
+
+// Generate and display 5 random integers.
+printfn "Five random integer values:"
+for _ = 0 to 4 do
+ printf $"{rand.Next(),15:N0}"
+printfn ""
+
+// Generate and display 5 random integers between 0 and 100.
+printfn "Five random integers between 0 and 100:"
+for _ = 0 to 4 do
+ printf $"{rand.Next 101,8:N0}"
+printfn ""
+
+// Generate and display 5 random integers from 50 to 100.
+printfn "Five random integers between 50 and 100:"
+for _ = 0 to 4 do
+ printf $"{rand.Next(50, 101),8:N0}"
+printfn ""
+
+// Generate and display 5 random floating point values from 0 to 1.
+printfn "Five Doubles."
+for _ = 0 to 4 do
+ printf $"{rand.NextDouble(),8:N3}"
+printfn ""
+
+// Generate and display 5 random floating point values from 0 to 5.
+printfn "Five Doubles between 0 and 5."
+for _ = 0 to 4 do
+ printf $"{rand.NextDouble() * 5.0,8:N3}"
+
+// The example displays output like the following:
+// Five random byte values:
+// 194 185 239 54 116
+// Five random integer values:
+// 507,353,531 1,509,532,693 2,125,074,958 1,409,512,757 652,767,128
+// Five random integers between 0 and 100:
+// 16 78 94 79 52
+// Five random integers between 50 and 100:
+// 56 66 96 60 65
+// Five Doubles.
+// 0.943 0.108 0.744 0.563 0.415
+// Five Doubles between 0 and 5.
+// 2.934 3.130 0.292 1.432 4.369
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
new file mode 100644
index 00000000000..e1d9186ad45
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
@@ -0,0 +1,19 @@
+open System
+
+//
+let 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" |]
+
+let rnd = Random()
+
+let index = rnd.Next(0,cities.Length)
+
+printfn "Today's city of the day: %s" cities.[index]
+
+// The example displays output like the following:
+// Today's city of the day: Honolulu
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
new file mode 100644
index 00000000000..8d8b63f69aa
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
@@ -0,0 +1,26 @@
+//
+open System
+
+type BooleanGenerator() =
+ let rnd = Random()
+
+ member _.NextBoolean() =
+ rnd.Next(0, 2) = 1
+
+let boolGen = BooleanGenerator()
+let mutable totalTrue, totalFalse = 0, 0
+
+for _ = 1 to 1000000 do
+ let value = boolGen.NextBoolean()
+ if value then
+ totalTrue <- totalTrue + 1
+ else
+ totalFalse <- totalFalse + 1
+
+printfn $"Number of true values: {totalTrue,7:N0} ({(double totalTrue) / double (totalTrue + totalFalse):P3})"
+printfn $"Number of false values: {totalFalse,7:N0} ({(double totalFalse) / double (totalTrue + totalFalse):P3})"
+
+// The example displays output like the following:
+// Number of true values: 500,004 (50.000 %)
+// Number of false values: 499,996 (50.000 %)
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
new file mode 100644
index 00000000000..0126e9feb37
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
@@ -0,0 +1,24 @@
+open System
+
+//
+let rnd = Random()
+
+let nextBool () =
+ rnd.Next(0, 2) = 1
+
+let mutable totalTrue, totalFalse = 0, 0
+
+for _ = 1 to 1000000 do
+ let value = nextBool ()
+ if value then
+ totalTrue <- totalTrue + 1
+ else
+ totalFalse <- totalFalse + 1
+
+printfn $"Number of true values: {totalTrue,7:N0} ({(double totalTrue) / double (totalTrue + totalFalse):P3})"
+printfn $"Number of false values: {totalFalse,7:N0} ({(double totalFalse) / double (totalTrue + totalFalse):P3})"
+
+// The example displays output like the following:
+// Number of true values: 499,777 (49.978 %)
+// Number of false values: 500,223 (50.022 %)
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs
new file mode 100644
index 00000000000..aecce4b42bf
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs
@@ -0,0 +1,47 @@
+//
+open System
+
+type Random2() =
+ inherit Random()
+
+ member this.NextBytes(bytes: byte[], minValue: byte, maxValue: byte) =
+ for i=bytes.GetLowerBound(0) to bytes.GetUpperBound(0) do
+ bytes.[i] <- this.Next(int minValue, int maxValue) |> byte
+
+let rnd = Random2()
+let bytes : byte [] = Array.zeroCreate 10000
+let total : int [] = Array.zeroCreate 101
+rnd.NextBytes(bytes, 0uy, 101uy)
+
+// Calculate how many of each value we have.
+for v in bytes do
+ total.[int v] <- total.[int v] + 1
+
+// Display the results.
+for i = 0 to total.Length - 1 do
+ printf "%3i: %-3i " i total.[i]
+ if (i + 1) % 5 = 0 then printfn ""
+
+// 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
+// 10: 91 11: 98 12: 106 13: 91 14: 92
+// 15: 101 16: 100 17: 96 18: 97 19: 100
+// 20: 101 21: 106 22: 112 23: 82 24: 85
+// 25: 102 26: 107 27: 98 28: 106 29: 102
+// 30: 109 31: 108 32: 94 33: 101 34: 107
+// 35: 101 36: 86 37: 100 38: 101 39: 102
+// 40: 113 41: 95 42: 96 43: 89 44: 99
+// 45: 81 46: 89 47: 105 48: 100 49: 85
+// 50: 103 51: 103 52: 93 53: 89 54: 91
+// 55: 97 56: 105 57: 97 58: 110 59: 86
+// 60: 116 61: 94 62: 117 63: 98 64: 110
+// 65: 93 66: 102 67: 100 68: 105 69: 83
+// 70: 81 71: 97 72: 85 73: 70 74: 98
+// 75: 100 76: 110 77: 114 78: 83 79: 90
+// 80: 96 81: 112 82: 102 83: 102 84: 99
+// 85: 81 86: 100 87: 93 88: 99 89: 118
+// 90: 95 91: 124 92: 108 93: 96 94: 104
+// 95: 106 96: 99 97: 99 98: 92 99: 99
+// 100: 108
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
new file mode 100644
index 00000000000..9d5a74a74d2
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
@@ -0,0 +1,35 @@
+open System
+
+//
+[]
+let ONE_TENTH = 922337203685477581L
+
+let rnd = Random()
+
+// Generate 20 million random integers.
+let count =
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (double Int64.MaxValue) )
+ |> Array.countBy (fun x -> x / (double ONE_TENTH) |> int ) // Categorize into 10 groups and count them.
+ |> Array.map snd
+
+// Display breakdown by range.
+printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
+for i = 0 to 9 do
+ let r1 = int64 i * ONE_TENTH
+ let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {double count.[i] / 20000000.0,7:P2}"
+
+// The example displays output like the following:
+// Range Count Pct.
+//
+// 0- 922,337,203,685,477,580 1,996,148 9.98 %
+// 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+// 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+// 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+// 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+// 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+// 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+// 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+// 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+// 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
new file mode 100644
index 00000000000..8637f3d32db
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
@@ -0,0 +1,20 @@
+open System
+
+//
+let rnd = Random()
+
+for _ = 1 to 10 do
+ printfn "%f" (rnd.NextDouble() - 1.0)
+
+// The example displays output like the following:
+// -0.930412760437658
+// -0.164699016215605
+// -0.9851692803135
+// -0.43468508843085
+// -0.177202483255976
+// -0.776813320245972
+// -0.0713201854710096
+// -0.0912875561468711
+// -0.540621722368813
+// -0.232211863730201
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
new file mode 100644
index 00000000000..a63dca2f0a8
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
@@ -0,0 +1,29 @@
+open System
+
+//
+let rnd = Random()
+
+let lowerBound = 10.0
+let upperBound = 11.0
+
+let range =
+ Array.init 1000000 (fun _ -> rnd.NextDouble() * (upperBound - lowerBound) + lowerBound)
+ |> Array.countBy (fun x -> Math.Truncate((x - lowerBound) * 10.0) |> int)
+ |> Array.map snd
+
+for i = 0 to 9 do
+ let lowerRange = 10.0 + float i * 0.1
+ printfn $"{lowerRange:N1} to {lowerRange + 0.1:N1}: {range.[i],8:N0} ({float range.[i] / 1000000.0,6:P2})"
+
+// The example displays output like the following:
+// 10.0 to 10.1: 99,929 ( 9.99 %)
+// 10.1 to 10.2: 100,189 (10.02 %)
+// 10.2 to 10.3: 99,384 ( 9.94 %)
+// 10.3 to 10.4: 100,240 (10.02 %)
+// 10.4 to 10.5: 99,397 ( 9.94 %)
+// 10.5 to 10.6: 100,580 (10.06 %)
+// 10.6 to 10.7: 100,293 (10.03 %)
+// 10.7 to 10.8: 100,135 (10.01 %)
+// 10.8 to 10.9: 99,905 ( 9.99 %)
+// 10.9 to 11.0: 99,948 ( 9.99 %)
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
new file mode 100644
index 00000000000..225fed52bf8
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
@@ -0,0 +1,35 @@
+open System
+
+//
+[]
+let ONE_TENTH = 922337203685477581L
+
+let rnd = Random()
+
+let count =
+ // Generate 20 million random long integers.
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (double Int64.MaxValue) |> int64 )
+ |> Array.countBy (fun x -> x / ONE_TENTH) // Categorize and count random numbers.
+ |> Array.map snd
+
+// Display breakdown by range.
+printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
+for i = 0 to 9 do
+ let r1 = int64 i * ONE_TENTH
+ let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {double count.[i] / 20000000.0,7:P2}"
+
+// The example displays output like the following:
+// Range Count Pct.
+//
+// 0- 922,337,203,685,477,580 1,996,148 9.98 %
+// 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+// 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+// 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+// 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+// 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+// 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+// 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+// 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+// 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
new file mode 100644
index 00000000000..23aeb5eb453
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
@@ -0,0 +1,15 @@
+open System
+
+//
+let rnd = Random()
+let bytes : Byte [] = Array.zeroCreate 20
+rnd.NextBytes bytes
+
+for i = 1 to bytes.Length do
+ printf "%3i " bytes.[i - 1]
+ if (i % 10 = 0) then printfn ""
+
+// The example displays output like the following:
+// 141 48 189 66 134 212 211 71 161 56
+// 181 166 220 133 9 252 222 57 62 62
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
new file mode 100644
index 00000000000..74586cf83fc
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
@@ -0,0 +1,14 @@
+open System
+
+//
+let rnd = Random()
+for i = 0 to 9 do
+ printf $"{rnd.NextDouble(),-19:R} "
+ if (i + 1) % 3 = 0 then printfn ""
+
+// The example displays output like the following:
+// 0.7911680553998649 0.0903414949264105 0.79776258291572455
+// 0.615568345233597 0.652644504165577 0.84023809378977776
+// 0.099662564741290441 0.91341467383942321 0.96018602045261581
+// 0.74772306473354022
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
new file mode 100644
index 00000000000..9c51119921c
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
@@ -0,0 +1,10 @@
+open System
+
+//
+let rnd = Random()
+for i = 0 to 9 do
+ printf "%3i " (rnd.Next(-10, 11))
+
+// The example displays output like the following:
+// 2 9 -3 2 4 -7 -3 -8 -8 5
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
new file mode 100644
index 00000000000..08b2a128612
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
@@ -0,0 +1,12 @@
+open System
+
+//
+let rnd = Random()
+for i = 1 to 15 do
+ printf "%3i " (rnd.Next(-10, 11))
+ if i % 5 = 0 then printfn ""
+// The example displays output like the following:
+// -2 -5 -1 -2 10
+// -3 6 -4 -8 3
+// -7 10 5 -2 4
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
new file mode 100644
index 00000000000..aed0ec61bbd
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
@@ -0,0 +1,16 @@
+open System
+
+//
+let rnd = Random()
+
+for i = 1 to 50 do
+ printf "%3i " (rnd.Next(1000,10000))
+ if i % 10 = 0 then printfn ""
+
+// The example displays output like the following:
+// 9570 8979 5770 1606 3818 4735 8495 7196 7070 2313
+// 5279 6577 5104 5734 4227 3373 7376 6007 8193 5540
+// 7558 3934 3819 7392 1113 7191 6947 4963 9179 7907
+// 3391 6667 7269 1838 7317 1981 5154 7377 3297 5320
+// 9869 8694 2684 4949 2999 3019 2357 5211 9604 2593
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
new file mode 100644
index 00000000000..6a0a75fa019
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
@@ -0,0 +1,78 @@
+//
+open System
+open System.IO
+
+let showRandomNumbers seed =
+ let rnd = Random seed
+ for _ = 0 to 20 do
+ printfn $"{rnd.NextDouble()}"
+
+let persistSeed (seed: int) =
+ use bin = new BinaryWriter(new FileStream(@".\seed.dat", FileMode.Create))
+ bin.Write seed
+
+let displayNewRandomNumbers () =
+ use bin = new BinaryReader(new FileStream(@".\seed.dat", FileMode.Open))
+ let seed = bin.ReadInt32()
+
+ let rnd = Random seed
+ for _ = 0 to 20 do
+ printfn $"{rnd.NextDouble()}"
+
+[]
+let main _ =
+ let seed = 100100
+ showRandomNumbers seed
+ printfn ""
+
+ persistSeed seed
+
+ displayNewRandomNumbers ()
+
+ 0
+
+// The example displays output like the following:
+// 0.500193602172748
+// 0.0209461245783354
+// 0.465869495396442
+// 0.195512794514891
+// 0.928583675496552
+// 0.729333720509584
+// 0.381455668891527
+// 0.0508996467343064
+// 0.019261200921266
+// 0.258578445417145
+// 0.0177532266908107
+// 0.983277184415272
+// 0.483650274334313
+// 0.0219647376900375
+// 0.165910115077118
+// 0.572085966622497
+// 0.805291457942357
+// 0.927985211335116
+// 0.4228545699375
+// 0.523320379910674
+// 0.157783938645285
+//
+// 0.500193602172748
+// 0.0209461245783354
+// 0.465869495396442
+// 0.195512794514891
+// 0.928583675496552
+// 0.729333720509584
+// 0.381455668891527
+// 0.0508996467343064
+// 0.019261200921266
+// 0.258578445417145
+// 0.0177532266908107
+// 0.983277184415272
+// 0.483650274334313
+// 0.0219647376900375
+// 0.165910115077118
+// 0.572085966622497
+// 0.805291457942357
+// 0.927985211335116
+// 0.4228545699375
+// 0.523320379910674
+// 0.157783938645285
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
new file mode 100644
index 00000000000..2b13bef4018
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
@@ -0,0 +1,141 @@
+//
+open System
+open System.Threading
+
+type Example() =
+ []
+ static val mutable private previous : double
+ []
+ static val mutable private perThreadCtr : int
+ []
+ static val mutable private perThreadTotal : double
+
+ static let source = new CancellationTokenSource()
+ static let countdown = new CountdownEvent 1
+ static let randLock = obj ()
+ static let numericLock = obj ()
+ static let rand = Random()
+ let mutable totalValue = 0.0
+ let mutable totalCount = 0
+
+ member _.GetRandomNumbers(token: CancellationToken) =
+ let mutable result = 0.0
+ countdown.AddCount 1
+ try
+ try
+ for _ = 0 to 1999999 do
+ // Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ lock randLock (fun () ->
+ result <- rand.NextDouble() )
+
+ // Check for corruption of Random instance.
+ if result = Example.previous && result = 0.0 then
+ source.Cancel()
+ else
+ Example.previous <- result
+
+ Example.perThreadCtr <- Example.perThreadCtr + 1
+ Example.perThreadTotal <- Example.perThreadTotal + result
+
+ // Update overall totals.
+ lock numericLock (fun () ->
+ // Show result.
+ printfn "Thread %s finished execution." Thread.CurrentThread.Name
+ printfn $"Random numbers generated: {Example.perThreadCtr:N0}"
+ printfn $"Sum of random numbers: {Example.perThreadTotal:N2}"
+ printfn $"Random number mean: {(Example.perThreadTotal / float Example.perThreadCtr):N4}\n"
+
+ // Update overall totals.
+ totalCount <- totalCount + Example.perThreadCtr
+ totalValue <- totalValue + Example.perThreadTotal)
+
+ with :? OperationCanceledException as e ->
+ printfn "Corruption in Thread %s %s" (e.GetType().Name) Thread.CurrentThread.Name
+ finally
+ countdown.Signal() |> ignore
+
+ member this.Execute() =
+ let token = source.Token
+ for i = 1 to 10 do
+ let newThread = Thread(fun () -> this.GetRandomNumbers token)
+ newThread.Name <- string i
+ newThread.Start()
+ this.GetRandomNumbers token
+
+ countdown.Signal() |> ignore
+
+ countdown.Wait()
+
+ source.Dispose()
+
+ printfn $"\nTotal random numbers generated: {totalCount:N0}"
+ printfn $"Total sum of all random numbers: {totalValue:N2}"
+ printfn $"Random number mean: {(totalValue / float totalCount):N4}"
+
+let ex = Example()
+Thread.CurrentThread.Name <- "Main"
+ex.Execute()
+
+// The example displays output like the following:
+// Thread 6 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,491.05
+// Random number mean: 0.5002
+//
+// Thread 10 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,329.64
+// Random number mean: 0.4997
+//
+// Thread 4 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,166.89
+// Random number mean: 0.5001
+//
+// Thread 8 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,628.37
+// Random number mean: 0.4998
+//
+// Thread Main finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,920.89
+// Random number mean: 0.5000
+//
+// Thread 3 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,370.45
+// Random number mean: 0.4997
+//
+// Thread 7 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,330.92
+// Random number mean: 0.4997
+//
+// Thread 9 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,172.79
+// Random number mean: 0.5001
+//
+// Thread 5 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,079.43
+// Random number mean: 0.5000
+//
+// Thread 1 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,817.91
+// Random number mean: 0.4999
+//
+// Thread 2 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,930.63
+// Random number mean: 0.5000
+//
+//
+// Total random numbers generated: 22,000,000
+// Total sum of all random numbers: 10,998,238.98
+// Random number mean: 0.4999
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
new file mode 100644
index 00000000000..754c2084f92
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
@@ -0,0 +1,138 @@
+//
+open System
+open System.Threading
+open System.Threading.Tasks
+
+type Example() =
+ static let source = new CancellationTokenSource()
+ static let rand = Random()
+
+ static let randLock = obj ()
+ static let numericLock = obj ()
+
+ let mutable totalValue = 0.0
+ let mutable totalCount = 0
+
+ member _.Execute() =
+ use source = source // Dispose of the cancellationTokenSource when we're done with it.
+ let token = source.Token
+ let tasks = [|
+ for i = 0 to 10 do
+ Task.Run(fun () ->
+ let mutable previous = 0.0
+ let mutable taskCtr = 0
+ let mutable taskTotal = 0.0
+ let mutable result = 0.0
+
+ for _ = 1 to 2000000 do
+ // Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ lock randLock (fun () ->
+ result <- rand.NextDouble() )
+
+ // Check for corruption of Random instance.
+ if result = previous && result = 0.0 then
+ source.Cancel()
+ else
+ previous <- result
+
+ taskCtr <- taskCtr + 1
+ taskTotal <- taskTotal + result
+
+ lock numericLock (fun () ->
+ // Show result.
+ printfn "Task %i finished execution." i
+ printfn $"Random numbers generated: {taskCtr:N0}"
+ printfn $"Sum of random numbers: {taskTotal:N2}"
+ printfn $"Random number mean: {(taskTotal / float taskCtr):N4}\n"
+
+ // Update overall totals.
+ totalCount <- totalCount + taskCtr
+ totalValue <- totalValue + taskTotal)
+ , token) |]
+ try
+ // Run tasks with F# Async.
+ Task.WhenAll tasks
+ |> Async.AwaitTask
+ |> Async.RunSynchronously
+
+ printfn $"\nTotal random numbers generated: {totalCount:N0}"
+ printfn $"Total sum of all random numbers: {totalValue:N2}"
+ printfn $"Random number mean: {(totalValue / float totalCount):N4}"
+ with
+ | :? AggregateException as e ->
+ for inner in e.InnerExceptions do
+ match inner with
+ | :? TaskCanceledException as canc ->
+ if canc <> null then
+ printfn $"Task #{canc.Task.Id} cancelled"
+ else
+ printfn $"Exception: {inner.GetType().Name}"
+ | _ -> ()
+
+let ex = Example()
+Thread.CurrentThread.Name <- "Main"
+ex.Execute()
+
+// The example displays output like the following:
+// Task 1 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,502.47
+// Random number mean: 0.5003
+//
+// Task 0 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,445.63
+// Random number mean: 0.5002
+//
+// Task 2 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,556.04
+// Random number mean: 0.5003
+//
+// Task 3 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,178.87
+// Random number mean: 0.5001
+//
+// Task 4 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,819.17
+// Random number mean: 0.4999
+//
+// Task 5 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,190.58
+// Random number mean: 0.5001
+//
+// Task 6 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,720.21
+// Random number mean: 0.4999
+//
+// Task 7 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,000.96
+// Random number mean: 0.4995
+//
+// Task 8 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,499.33
+// Random number mean: 0.4997
+//
+// Task 9 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,193.25
+// Random number mean: 0.5001
+//
+// Task 10 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,960.82
+// Random number mean: 0.5000
+//
+//
+// Total random numbers generated: 22,000,000
+// Total sum of all random numbers: 11,000,067.33
+// Random number mean: 0.5000
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
new file mode 100644
index 00000000000..7515f8ba08b
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
@@ -0,0 +1,44 @@
+//
+open System
+open System.Threading
+
+printfn "Instantiating two random number generators..."
+let rnd1 = Random()
+Thread.Sleep 2000
+let rnd2 = Random()
+
+printfn "\nThe first random number generator:"
+for _ = 1 to 10 do
+ printfn $" {rnd1.Next()}"
+
+printfn "\nThe second random number generator:"
+for _ = 1 to 10 do
+ printfn $" {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
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
new file mode 100644
index 00000000000..d25be630563
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
@@ -0,0 +1,76 @@
+//
+open System
+
+type Suit =
+ | Clubs
+ | Diamonds
+ | Hearts
+ | Spades
+
+type Face =
+ | Ace | Two | Three
+ | Four | Five | Six
+ | Seven | Eight | Nine
+ | Ten | Jack | Queen | King
+
+type Card = { Face: Face; Suit: Suit }
+
+let suits = [Clubs; Diamonds; Hearts; Spades]
+let faces = [Ace; Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten; Jack; Queen; King]
+
+type Dealer() =
+ let rnd = Random()
+ let mutable pos = 0
+ // Parallel array for sorting cards.
+ let order = Array.init (suits.Length * faces.Length) (fun _ -> rnd.NextDouble() )
+ // A deck of cards, without Jokers.
+ let deck = [|
+ for s in suits do
+ for f in faces do
+ { Face = f; Suit = s } |]
+ // Shuffle the deck.
+ do Array.Sort(order, deck)
+
+ // Deal a number of cards from the deck, return None if failed
+ member _.Deal(numberToDeal) : Card [] option =
+ if numberToDeal = 0 || pos = deck.Length then
+ printfn "There are no cards left in the deck"
+ None
+ else
+ let cards = deck.[pos .. numberToDeal + pos - 1]
+ if numberToDeal > deck.Length - pos then
+ printfn "Can only deal the %i cards remaining on the deck." (deck.Length - pos)
+ pos <- min (pos + numberToDeal) deck.Length
+ Some cards
+
+let showCards cards =
+ for card in cards do
+ printfn $"{card.Face} of {card.Suit}"
+
+let dealer = Dealer()
+
+dealer.Deal 20
+|> Option.iter showCards
+
+// The example displays output like the following:
+// Six of Diamonds
+// King of Clubs
+// Eight of Clubs
+// Seven of Clubs
+// Queen of Clubs
+// King of Hearts
+// Three of Spades
+// Ace of Clubs
+// Four of Hearts
+// Three of Diamonds
+// Nine of Diamonds
+// Two of Hearts
+// Ace of Hearts
+// Three of Hearts
+// Four of Spades
+// Eight of Hearts
+// Queen of Diamonds
+// Two of Clubs
+// Four of Diamonds
+// Jack of Hearts
+//
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index 6f426f77418..dd26d9050ab 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -98,6 +98,7 @@
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/random1.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/Random1.cs" interactive="try-dotnet-method" id="Snippet1":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/Random1.vb" id="Snippet1":::
To avoid this problem, create a single object instead of multiple objects. Note that the `Random` class in .NET Core does not have this limitation.
@@ -118,6 +119,7 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/threadsafeex1.cpp" id="Snippet3":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/threadsafeex1.cs" id="Snippet3":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs" id="Snippet3":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/threadsafeex1.vb" id="Snippet3":::
The example ensures thread-safety in the following ways:
@@ -135,6 +137,7 @@ To avoid this problem, create a single object instead of mu
The following example is identical to the first, except that it uses a object and a lambda expression instead of objects.
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/threadsafeex2.cs" id="Snippet4":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/threadsafeex2.vb" id="Snippet4":::
It differs from the first example in the following ways:
@@ -153,6 +156,7 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/nextbytes1.cpp" id="Snippet5":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/nextbytes1.cs" interactive="try-dotnet-method" id="Snippet5":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs" id="Snippet5":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/nextbytes1.vb" id="Snippet5":::
- A single integer. You can choose whether you want an integer from 0 to a maximum value ( - 1) by calling the method, an integer between 0 and a specific value by calling the method, or an integer within a range of values by calling the method. In the parameterized overloads, the specified maximum value is exclusive; that is, the actual maximum number generated is one less than the specified value.
@@ -161,12 +165,14 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/nextex1.cpp" id="Snippet6":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/nextex1.cs" interactive="try-dotnet-method" id="Snippet6":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs" id="Snippet6":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/nextex1.vb" id="Snippet6":::
- A single floating-point value from 0.0 to less than 1.0 by calling the method. The exclusive upper bound of the random number returned by the method is 1, so its actual upper bound is 0.99999999999999978. The following example generates 10 random floating-point numbers.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/nextdoubleex1.cpp" id="Snippet7":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/nextdoubleex1.cs" interactive="try-dotnet-method" id="Snippet7":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs" id="Snippet7":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/nextdoubleex1.vb" id="Snippet7":::
> [!IMPORTANT]
@@ -191,6 +197,7 @@ To avoid this problem, create a single object instead of mu
You can generate the same sequence of random numbers by providing the same seed value to the constructor. The seed value provides a starting value for the pseudo-random number generation algorithm. The following example uses 100100 as an arbitrary seed value to instantiate the object, displays 20 random floating-point values, and persists the seed value. It then restores the seed value, instantiates a new random number generator, and displays the same 20 random floating-point values. Note that the example may produce different sequences of random numbers if run on different versions of the .NET Framework.
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/same1.cs" interactive="try-dotnet" id="Snippet12":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs" id="Snippet12":::
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/same1.cpp" id="Snippet12":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/same1.vb" id="Snippet12":::
@@ -200,6 +207,7 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/unique.cpp" id="Snippet13":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/unique.cs" interactive="try-dotnet" id="Snippet13":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs" id="Snippet13":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/unique.vb" id="Snippet13":::
However, because of its finite resolution, the system clock doesn't detect time differences that are less than approximately 15 milliseconds. Therefore, if your code calls the overload on the .NET Framework to instantiate two objects in succession, you might inadvertently be providing the objects with identical seed values. (The class in .NET Core does not have this limitation.) To see this in the previous example, comment out the method call, and compile and run the example again.
@@ -212,6 +220,7 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/range1.cpp" id="Snippet15":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/range1.cs" interactive="try-dotnet-method" id="Snippet15":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs" id="Snippet15":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/range1.vb" id="Snippet15":::
@@ -220,6 +229,7 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/range2.cpp" id="Snippet16":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/range2.cs" interactive="try-dotnet-method" id="Snippet16":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs" id="Snippet16":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/range2.vb" id="Snippet16":::
@@ -230,12 +240,14 @@ To avoid this problem, create a single object instead of mu
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/doublerange2.cpp" id="Snippet17":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/doublerange2.cs" interactive="try-dotnet-method" id="Snippet17":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs" id="Snippet17":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/doublerange2.vb" id="Snippet17":::
To generate random floating-point numbers whose lower bound is 0 but upper bound is greater than 1 (or, in the case of negative numbers, whose lower bound is less than -1 and upper bound is 0), multiply the random number by the non-zero bound. The following example does this to generate 20 million random floating-point numbers that range from 0 to . In also displays the distribution of the random values generated by the method.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/doublerange1.cpp" id="Snippet18":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/doublerange1.cs" interactive="try-dotnet-method" id="Snippet18":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs" id="Snippet18":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/doublerange1.vb" id="Snippet18":::
To generate random floating-point numbers between two arbitrary values, like the method does for integers, use the following formula:
@@ -248,6 +260,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/doublerange3.cpp" id="Snippet19":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/doublerange3.cs" interactive="try-dotnet-method" id="Snippet19":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs" id="Snippet19":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/doublerange3.vb" id="Snippet19":::
@@ -256,12 +269,14 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/booleans1.cpp" id="Snippet8":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/booleans1.cs" interactive="try-dotnet" id="Snippet8":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs" id="Snippet8":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/booleans1.vb" id="Snippet8":::
Instead of creating a separate class to generate random values, the example could simply have defined a single method. In that case, however, the object should have been defined as a class-level variable to avoid instantiating a new instance in each method call. In Visual Basic, the Random instance can be defined as a [Static](/dotnet/visual-basic/language-reference/modifiers/static) variable in the `NextBoolean` method. The following example provides an implementation.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/booleans2.cpp" id="Snippet20":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/booleans2.cs" interactive="try-dotnet-method" id="Snippet20":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs" id="Snippet20":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/booleans2.vb" id="Snippet20":::
@@ -276,6 +291,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/long1.cpp" id="Snippet14":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/long1.cs" interactive="try-dotnet-method" id="Snippet14":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs" id="Snippet14":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/long1.vb" id="Snippet14":::
An alternative technique that uses bit manipulation does not generate truly random numbers. This technique calls to generate two integers, left-shifts one by 32 bits, and ORs them together. This technique has two limitations:
@@ -290,6 +306,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/bytes1.cpp" id="Snippet9":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/bytes1.cs" interactive="try-dotnet" id="Snippet9":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs" id="Snippet9":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/bytes1.vb" id="Snippet9":::
The `NextBytes(Byte[], Byte, Byte)` method wraps a call to the method and specifies the minimum value and one greater than the maximum value (in this case, 0 and 101) that we want returned in the byte array. Because we are sure that the integer values returned by the method are within the range of the data type, we can safely cast them (in C#) or convert them (in Visual Basic) from integers to bytes.
@@ -300,6 +317,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/array1.cpp" id="Snippet10":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/array1.cs" interactive="try-dotnet-method" id="Snippet10":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs" id="Snippet10":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/array1.vb" id="Snippet10":::
@@ -314,6 +332,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/uniquearray1.cpp" id="Snippet11":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/uniquearray1.cs" interactive="try-dotnet" id="Snippet11":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs" id="Snippet11":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/uniquearray1.vb" id="Snippet11":::
## Examples
@@ -322,6 +341,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/random2.cpp" id="Snippet2":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/Random2.cs" interactive="try-dotnet-method" id="Snippet2":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/Random2.vb" id="Snippet2":::
The following example generates a random integer that it uses as an index to retrieve a string value from an array.
From 1acd577eb64231c0777f2bd205983412798d0104 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 19:16:54 -0700
Subject: [PATCH 02/14] rename file
---
.../system.Random/fs/{bytes.fs => bytes1.fs} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/{bytes.fs => bytes1.fs} (100%)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
similarity index 100%
rename from samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes.fs
rename to samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
From e67b8b9162171c29421d104c337838c110670d1b Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 20:04:04 -0700
Subject: [PATCH 03/14] system.Random.Ctor snippets
---
.../system.Random.Ctor/FS/ctor.fs | 97 +++++++++++++++++++
.../system.Random.Ctor/FS/ctor1.fs | 27 ++++++
.../system.Random.Ctor/FS/ctor4.fs | 27 ++++++
xml/System/Random.xml | 4 +
4 files changed, 155 insertions(+)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
new file mode 100644
index 00000000000..cde1c280acc
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
@@ -0,0 +1,97 @@
+//
+// Example of the Random class constructors and Random.NextDouble()
+// method.
+open System
+open System.Threading
+
+// Generate random numbers from the specified Random object.
+let runIntNDoubleRandoms (randObj: Random) =
+ // Generate the first six random integers.
+ for _ = 1 to 6 do
+ printf $" {randObj.Next(),10} "
+ printfn ""
+
+ // Generate the first six random doubles.
+ for _ = 1 to 6 do
+ printf $" {randObj.NextDouble():F8} "
+ printfn ""
+
+let fixedSeedRandoms seed =
+ printfn "\nRandom numbers from a Random object with seed = %i:" seed
+ let fixRand = Random seed
+
+ runIntNDoubleRandoms fixRand
+
+let autoSeedRandoms () =
+ // Wait to allow the timer to advance.
+ Thread.Sleep 1
+
+ printfn "\nRandom numbers from a Random object with an auto-generated seed: "
+ let autoRand = Random ()
+
+ runIntNDoubleRandoms autoRand
+
+printfn "This example of the Random class constructors and Random.NextDouble()"
+printfn "generates the following output.\n"
+printfn "Create Random objects, and then generate and display six integers and"
+printfn "six doubles from each."
+
+fixedSeedRandoms 123
+fixedSeedRandoms 123
+
+fixedSeedRandoms 456
+fixedSeedRandoms 456
+
+autoSeedRandoms ()
+autoSeedRandoms ()
+autoSeedRandoms ()
+
+(*
+This example of the Random class constructors and Random.NextDouble()
+generates an output similar to the following:
+
+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
+*)
+//
+
+// Code added to show how to initialize Random objects with the
+// same timer value that will produce unique random number sequences.
+let createEngineWithSameTimer () =
+//
+ let randomEngines =
+ Array.init 4 (fun i ->
+ Operators.(>>>) DateTime.Now.Ticks i
+ |> int
+ |> Random )
+//
+ for i in randomEngines do
+ printfn "%i" (i.Next())
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
new file mode 100644
index 00000000000..9add40bcf3c
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
@@ -0,0 +1,27 @@
+//
+open System
+open System.Threading
+
+let showRandomNumbers (rand: Random) =
+ printfn ""
+ let values : byte [] = Array.zeroCreate 5
+ rand.NextBytes values
+ for value in values do
+ printf "%5i" value
+ printfn ""
+
+let rand1 = Random()
+let rand2 = Random()
+Thread.Sleep 2000
+let rand3 = Random()
+showRandomNumbers rand1
+showRandomNumbers rand2
+showRandomNumbers rand3
+
+// The example displays an output similar to the following:
+// 28 35 133 224 58
+//
+// 28 35 133 224 58
+//
+// 32 222 43 251 49
+//
\ No newline at end of file
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
new file mode 100644
index 00000000000..1e438519a5d
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
@@ -0,0 +1,27 @@
+//
+open System
+open System.Threading
+
+let showRandomNumbers (rand: Random) =
+ printfn ""
+ let values : byte [] = Array.zeroCreate 5
+ rand.NextBytes values
+ for value in values do
+ printf "%5i" value
+ printfn ""
+
+let rand1 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
+let rand2 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
+Thread.Sleep 20
+let rand3 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
+showRandomNumbers rand1
+showRandomNumbers rand2
+showRandomNumbers rand3
+
+// The example displays output similar to the following:
+// 145 214 177 134 173
+//
+// 145 214 177 134 173
+//
+// 126 185 175 249 157
+//
\ No newline at end of file
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index dd26d9050ab..be623fd75af 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -429,6 +429,7 @@ In .NET Core, the default seed value is produced by the thread-static, pseudo-ra
The following example uses the parameterless constructor to instantiate three objects and displays a sequence of five random integers for each. If it is run on .NET Framework, because the first two objects are created in close succession, they are instantiated using identical seed values based on the system clock and, therefore, they produce an identical sequence of random numbers. On the other hand, the parameterless constructor of the third object is called after a two-second delay caused by calling the method. Because this produces a different seed value for the third object, it produces a different sequence of random numbers.
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Ctor/CS/ctor1.cs" interactive="try-dotnet" id="Snippet2":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Ctor/VB/ctor1.vb" id="Snippet2":::
]]>
@@ -484,6 +485,7 @@ The following example uses the parameterless constructor to instantiate three overload does. However, the system clock might not have sufficient resolution to provide different invocations of this constructor with a different seed value. On the .NET Framework, this results in random number generators that generate identical sequences of pseudo-random numbers, as illustrated by the first two objects in the following example. To prevent this, apply an algorithm to differentiate the seed value in each invocation, or call the method to ensure that you provide each constructor with a different seed value.
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Ctor/CS/ctor4.cs" interactive="try-dotnet" id="Snippet4":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Ctor/VB/ctor4.vb" id="Snippet4":::
Another option is to instantiate a single object that you use to generate all the random numbers in your application. This yields slightly better performance, since instantiating a random number generator is fairly expensive.
@@ -495,6 +497,7 @@ The following example uses the parameterless constructor to instantiate three
@@ -922,6 +925,7 @@ Each element of the span of bytes is set to a random number greater than or equa
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random.Ctor/CPP/ctor.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Ctor/CS/ctor.cs" interactive="try-dotnet" id="Snippet1":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Ctor/VB/ctor.vb" id="Snippet1":::
The following example calls the method to generate 100 random numbers and displays their frequency distribution.
From f95e3c8977ec7c84ac920f46ba111658a87a27cd Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 20:41:34 -0700
Subject: [PATCH 04/14] system.Random.Next snippets
---
.../system.Random.Next/FS/Next2.fs | 39 +++++++++
.../system.Random.Next/FS/next.fs | 87 +++++++++++++++++++
.../system.Random.Next/FS/next1.fs | 28 ++++++
.../system.Random.Next/FS/next3.fs | 24 +++++
.../system.Random.Next/FS/next4.fs | 28 ++++++
xml/System/Random.xml | 6 ++
6 files changed, 212 insertions(+)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
new file mode 100644
index 00000000000..00b18b70681
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
@@ -0,0 +1,39 @@
+open System
+
+//
+let rnd = Random()
+
+printfn "\n20 random integers from -100 to 100:"
+for i = 1 to 20 do
+ printf "%6i" (rnd.Next(-100,100))
+ if i % 5 = 0 then printfn ""
+
+printfn "\n20 random integers from 1000 to 10000:"
+for i = 1 to 20 do
+ printf "%8i" (rnd.Next(1000,10001))
+ if i % 5 = 0 then printfn ""
+
+printfn "\n20 random integers from 1 to 10:"
+for i = 1 to 20 do
+ printf "%6i" (rnd.Next(1,11))
+ if i % 5 = 0 then printfn ""
+
+// The example displays output similar to the following:
+// 20 random integers from -100 to 100:
+// 65 -95 -10 90 -35
+// -83 -16 -15 -19 41
+// -67 -93 40 12 62
+// -80 -95 67 -81 -21
+//
+// 20 random integers from 1000 to 10000:
+// 4857 9897 4405 6606 1277
+// 9238 9113 5151 8710 1187
+// 2728 9746 1719 3837 3736
+// 8191 6819 4923 2416 3028
+//
+// 20 random integers from 1 to 10:
+// 9 8 5 9 9
+// 9 1 2 3 8
+// 1 4 8 10 5
+// 9 7 9 10 5
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
new file mode 100644
index 00000000000..da81c55a94e
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
@@ -0,0 +1,87 @@
+// Example of the Random.Next() methods.
+open System
+
+//
+let noBoundsRandoms seed =
+ printfn "\nRandom object, seed = %i, no bounds:" seed
+ let randObj = Random seed
+
+ // Generate six random integers from 0 to int.MaxValue.
+ for _ = 1 to 6 do
+ printf "%11i " (randObj.Next())
+ printfn ""
+
+// Generate random numbers with an upper bound specified.
+let upperBoundRandoms seed upper =
+ printfn "\nRandom object, seed = %i, upper bound = %i" seed upper
+ let randObj = Random seed
+
+ // Generate six random integers from 0 to the upper bound.
+ for _ = 1 to 6 do
+ printf "%11i " (randObj.Next(upper))
+ printfn ""
+
+// Generate random numbers with both bounds specified.
+let bothBoundRandoms seed lower upper =
+ printfn "\nRandom object, seed = %i, lower = %i, upper = %i: " seed lower upper
+ let randObj = Random seed
+
+ // Generate six random integers from the lower to upper bounds.
+ for _ = 1 to 6 do
+ printf "%11i " (randObj.Next(lower,upper))
+ printfn ""
+
+printfn "This example of the Random.Next() methods\ngenerates the following.\n"
+
+printfn """Create Random objects all with the same seed and generate
+sequences of numbers with different bounds. Note the effect
+that the various combinations of bounds have on the sequences."""
+
+noBoundsRandoms 234
+
+upperBoundRandoms 234 Int32.MaxValue
+upperBoundRandoms 234 2000000000
+upperBoundRandoms 234 200000000
+
+bothBoundRandoms 234 0 Int32.MaxValue
+bothBoundRandoms 234 Int32.MinValue Int32.MaxValue
+bothBoundRandoms 234 -2000000000 2000000000
+bothBoundRandoms 234 -200000000 200000000
+bothBoundRandoms 234 -2000 2000
+
+(*
+This example of the Random.Next() methods
+generates the following output.
+
+Create Random objects all with the same seed and generate
+sequences of numbers with different bounds. Note the effect
+that the various combinations of bounds have on the sequences.
+
+Random object, seed = 234, no bounds:
+2091148258 1024955023 711273344 1081917183 1833298756 109460588
+
+Random object, seed = 234, upper bound = 2147483647:
+2091148258 1024955023 711273344 1081917183 1833298756 109460588
+
+Random object, seed = 234, upper bound = 2000000000:
+1947533580 954563751 662424922 1007613896 1707392518 101943116
+
+Random object, seed = 234, upper bound = 200000000:
+194753358 95456375 66242492 100761389 170739251 10194311
+
+Random object, seed = 234, lower = 0, upper = 2147483647:
+2091148258 1024955023 711273344 1081917183 1833298756 109460588
+
+Random object, seed = 234, lower = -2147483648, upper = 2147483647:
+2034812868 -97573602 -724936960 16350718 1519113864 -1928562472
+
+Random object, seed = 234, lower = -2000000000, upper = 2000000000:
+1895067160 -90872498 -675150156 15227793 1414785036 -1796113767
+
+Random object, seed = 234, lower = -200000000, upper = 200000000:
+189506716 -9087250 -67515016 1522779 141478503 -179611377
+
+Random object, seed = 234, lower = -2000, upper = 2000:
+ 1895 -91 -676 15 1414 -1797
+*)
+//
\ No newline at end of file
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
new file mode 100644
index 00000000000..a802626466e
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
@@ -0,0 +1,28 @@
+open System
+
+//
+let rnd = Random()
+
+let malePetNames =
+ [| "Rufus"; "Bear"; "Dakota"; "Fido";
+ "Vanya"; "Samuel"; "Koani"; "Volodya";
+ "Prince"; "Yiska" |]
+let femalePetNames =
+ [| "Maggie"; "Penny"; "Saya"; "Princess";
+ "Abby"; "Laila"; "Sadie"; "Olivia";
+ "Starlight"; "Talla" |]
+
+// Generate random indexes for pet names.
+let mIndex = rnd.Next malePetNames.Length
+let fIndex = rnd.Next femalePetNames.Length
+
+// Display the result.
+printfn "Suggested pet name of the day: "
+printfn " For a male: %s" malePetNames.[mIndex]
+printfn " For a female: %s" femalePetNames.[fIndex]
+
+// The example displays output similar to the following:
+// Suggested pet name of the day:
+// For a male: Koani
+// For a female: Maggie
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
new file mode 100644
index 00000000000..ec804b445a1
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
@@ -0,0 +1,24 @@
+open System
+
+//
+let rnd = Random()
+
+printfn "Generating 10 random numbers:"
+
+for _ = 1 to 10 do
+ printfn $"{rnd.Next(),15:N0}"
+
+// The example displays output like the following:
+//
+// Generating 10 random numbers:
+// 1,733,189,596
+// 566,518,090
+// 1,166,108,546
+// 1,931,426,514
+// 1,532,939,448
+// 762,207,767
+// 815,074,920
+// 1,521,208,785
+// 1,950,436,671
+// 1,266,596,666
+//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
new file mode 100644
index 00000000000..65abb4c82b5
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
@@ -0,0 +1,28 @@
+open System
+
+//
+let rnd = Random()
+
+let malePetNames =
+ [| "Rufus"; "Bear"; "Dakota"; "Fido";
+ "Vanya"; "Samuel"; "Koani"; "Volodya";
+ "Prince"; "Yiska" |]
+let femalePetNames =
+ [| "Maggie"; "Penny"; "Saya"; "Princess";
+ "Abby"; "Laila"; "Sadie"; "Olivia";
+ "Starlight"; "Talla" |]
+
+// Generate random indexes for pet names.
+let mIndex = rnd.Next(0, malePetNames.Length)
+let fIndex = rnd.Next(0, femalePetNames.Length)
+
+// Display the result.
+printfn "Suggested pet name of the day: "
+printfn " For a male: %s" malePetNames.[mIndex]
+printfn " For a female: %s" femalePetNames.[fIndex]
+
+// The example displays output similar to the following:
+// Suggested pet name of the day:
+// For a male: Koani
+// For a female: Maggie
+//
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index be623fd75af..f370e555549 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -348,6 +348,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random.Next/CPP/next1.cpp" id="Snippet3":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Next/CS/next1.cs" interactive="try-dotnet-method" id="Snippet3":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs" id="Snippet3":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Next/VB/next1.vb" id="Snippet3":::
]]>
@@ -577,6 +578,7 @@ The following example uses the parameterless constructor to instantiate three to generate a sequence of random numbers whose distribution differs from the uniform distribution generated by the method of the base class. It overrides the method to provide the distribution of random numbers, and overrides the method to use series of random numbers.
@@ -650,12 +652,14 @@ The following example uses the parameterless constructor to instantiate three property is supplied as a the `maxValue` parameter.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random.Next/CPP/next1.cpp" id="Snippet3":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Next/CS/next1.cs" interactive="try-dotnet-method" id="Snippet3":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs" id="Snippet3":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Next/VB/next1.vb" id="Snippet3":::
]]>
@@ -726,12 +730,14 @@ The following example uses the parameterless constructor to instantiate three property is supplied as a the `maxValue` parameter.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random.Next/CPP/next4.cpp" id="Snippet4":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Next/CS/next4.cs" interactive="try-dotnet-method" id="Snippet4":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Next/VB/next4.vb" id="Snippet4":::
]]>
From 875fc7ef585da8cda62fe135dd438ef3cb821697 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 20:58:30 -0700
Subject: [PATCH 05/14] system.random.nextdouble snippet
---
.../fs/nextdouble1.fs | 29 +++++++++++++++++++
xml/System/Random.xml | 1 +
2 files changed, 30 insertions(+)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fs
new file mode 100644
index 00000000000..496c487745a
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fs
@@ -0,0 +1,29 @@
+open System
+
+//
+let rnd = Random()
+
+let frequency =
+ Array.init 100 (fun _ ->
+ let number = rnd.NextDouble()
+ floor (number * 10.0) |> int )
+ |> Array.countBy id
+ |> Array.map snd
+
+printfn "Distribution of Random Numbers:"
+for i = frequency.GetLowerBound 0 to frequency.GetUpperBound 0 do
+ printfn $"0.{i}0-0.{i}9 {frequency.[i]}"
+
+// The following example displays output similar to the following:
+// Distribution of Random Numbers:
+// 0.00-0.09 16
+// 0.10-0.19 8
+// 0.20-0.29 8
+// 0.30-0.39 11
+// 0.40-0.49 9
+// 0.50-0.59 6
+// 0.60-0.69 13
+// 0.70-0.79 6
+// 0.80-0.89 9
+// 0.90-0.99 14
+//
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index f370e555549..62055aa02d8 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -937,6 +937,7 @@ Each element of the span of bytes is set to a random number greater than or equa
The following example calls the method to generate 100 random numbers and displays their frequency distribution.
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.random.nextdouble/cs/nextdouble1.cs" interactive="try-dotnet-method" id="Snippet2":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.random.nextdouble/vb/nextdouble1.vb" id="Snippet2":::
]]>
From 5ae5a0f554c335d6f1a81d17138f59605a7c3130 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 21:55:51 -0700
Subject: [PATCH 06/14] system.Random.Sample snippet
---
.../system.Random.Sample/FS/sample.fs | 128 ++++++++++++++++++
xml/System/Random.xml | 1 +
2 files changed, 129 insertions(+)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
new file mode 100644
index 00000000000..f97a0064217
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
@@ -0,0 +1,128 @@
+//
+open System
+
+// This derived class converts the uniformly distributed random
+// numbers generated by base.Sample() to another distribution.
+
+type RandomProportional() =
+ inherit Random()
+
+ // The Sample method generates a distribution proportional to the value
+ // of the random numbers, in the range [0.0, 1.0].
+ override _.Sample() =
+ sqrt (base.Sample())
+
+ override this.Next() =
+ this.Sample() * float Int32.MaxValue
+ |> int
+
+let [] rows = 4
+let [] cols = 6
+let [] runCount = 1000000
+let [] distGroupCount = 10
+
+let intGroupSize =
+ (float Int32.MaxValue + 1.0) / float distGroupCount
+
+let randObj = RandomProportional()
+
+printfn """
+The derived RandomProportional class overrides the Sample method to
+generate random numbers in the range [0.0, 1.0]. The distribution
+of the numbers is proportional to their numeric values. For example,
+numbers are generated in the vicinity of 0.75 with three times the
+probability of those generated near 0.25."""
+
+printfn "\nRandom doubles generated with the NextDouble() method:\n"
+
+// Generate and display [rows * cols] random doubles.
+for _ = 1 to rows do
+ for _ = 1 to cols do
+ printf $"{randObj.NextDouble(),12:F8}"
+ printfn ""
+
+printfn "\nRandom integers generated with the Next() method:\n"
+// Generate and display [rows * cols] random integers.
+for _ = 1 to rows do
+ for _ = 1 to cols do
+ printf $"{randObj.Next(),12}"
+ printfn ""
+
+printfn $"""
+To demonstrate the proportional distribution,
+{runCount:N0} random
+integers and doubles are grouped into {distGroupCount} equal value ranges. This
+is the count of values in each range:
+"""
+
+printfn $"""{"Integer Range",21}{"Count",10}{"Double Range",20}{"Count",10}"""
+printfn $"""{"-------------",21}{"-----",10}{"------------",20}{"-----",10}"""
+
+// Generate random integers and doubles, and then count them by group.
+let intCounts =
+ Array.init runCount (fun _ ->
+ (randObj.Next() |> float) / float intGroupSize
+ |> int )
+ |> Array.countBy id
+ |> Array.map snd
+
+let realCounts =
+ Array.init runCount (fun _ ->
+ randObj.NextDouble() * float intGroupSize
+ |> int )
+ |> Array.countBy id
+ |> Array.map snd
+
+// Display the count of each group.
+for i = 1 to distGroupCount - 1 do
+ Console.WriteLine(
+ "{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}",
+ (int)((float)i * intGroupSize),
+ (int)((float)(i + 1) * intGroupSize - 1.0),
+ intCounts.[i],
+ ((float)i) / (float)distGroupCount,
+ ((float)(i + 1)) / (float)distGroupCount,
+ realCounts.[i])
+
+(*
+This example of Random.Sample() displays output similar to the following:
+
+ The derived RandomProportional class overrides the Sample method to
+ generate random numbers in the range [0.0, 1.0). The distribution
+ of the numbers is proportional to the number values. For example,
+ numbers are generated in the vicinity of 0.75 with three times the
+ probability of those generated near 0.25.
+
+ Random doubles generated with the NextDouble() method:
+
+ 0.59455719 0.17589882 0.83134398 0.35795862 0.91467727 0.54022658
+ 0.93716947 0.54817519 0.94685080 0.93705478 0.18582318 0.71272428
+ 0.77708682 0.95386216 0.70412393 0.86099417 0.08275804 0.79108316
+ 0.71019941 0.84205103 0.41685082 0.58186880 0.89492302 0.73067715
+
+ Random integers generated with the Next() method:
+
+ 1570755704 1279192549 1747627711 1705700211 1372759203 1849655615
+ 2046235980 1210843924 1554274149 1307936697 1480207570 1057595022
+ 337854215 844109928 2028310798 1386669369 2073517658 1291729809
+ 1537248240 1454198019 1934863511 1640004334 2032620207 534654791
+
+ To demonstrate the proportional distribution, 1,000,000 random
+ integers and doubles are grouped into 10 equal value ranges. This
+ is the count of values in each range:
+
+ Integer Range Count Double Range Count
+ ------------- ----- ------------ -----
+ 0- 214748363 10,079 0.00000-0.10000 10,148
+ 214748364- 429496728 29,835 0.10000-0.20000 29,849
+ 429496729- 644245093 49,753 0.20000-0.30000 49,948
+ 644245094- 858993458 70,325 0.30000-0.40000 69,656
+ 858993459-1073741823 89,906 0.40000-0.50000 90,337
+ 1073741824-1288490187 109,868 0.50000-0.60000 110,225
+ 1288490188-1503238552 130,388 0.60000-0.70000 129,986
+ 1503238553-1717986917 149,231 0.70000-0.80000 150,428
+ 1717986918-1932735282 170,234 0.80000-0.90000 169,610
+ 1932735283-2147483647 190,381 0.90000-1.00000 189,813
+*)
+//
+
\ No newline at end of file
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index 62055aa02d8..3fac9a85487 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -1143,6 +1143,7 @@ Each element of the span of bytes is set to a random number greater than or equa
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random.Sample/cpp/sampleex.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random.Sample/CS/sample.cs" interactive="try-dotnet" id="Snippet1":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random.Sample/VB/sample.vb" id="Snippet1":::
]]>
From 1790a92960bdea42d4abf6177aa4509a071da1e6 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 22:01:28 -0700
Subject: [PATCH 07/14] double -> float
F# floats are 64bit and equivalent to doubles
changed for consistency
---
.../VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs | 4 ++--
.../fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs | 4 ++--
.../VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs | 4 ++--
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
index 9d5a74a74d2..5b6def11e1d 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
@@ -9,7 +9,7 @@ let rnd = Random()
// Generate 20 million random integers.
let count =
Array.init 20000000 (fun _ -> rnd.NextDouble() * (double Int64.MaxValue) )
- |> Array.countBy (fun x -> x / (double ONE_TENTH) |> int ) // Categorize into 10 groups and count them.
+ |> Array.countBy (fun x -> x / (float ONE_TENTH) |> int ) // Categorize into 10 groups and count them.
|> Array.map snd
// Display breakdown by range.
@@ -17,7 +17,7 @@ printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
for i = 0 to 9 do
let r1 = int64 i * ONE_TENTH
let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
- printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {double count.[i] / 20000000.0,7:P2}"
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {float count.[i] / 20000000.0,7:P2}"
// The example displays output like the following:
// Range Count Pct.
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
index 225fed52bf8..240528abcfe 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
@@ -8,7 +8,7 @@ let rnd = Random()
let count =
// Generate 20 million random long integers.
- Array.init 20000000 (fun _ -> rnd.NextDouble() * (double Int64.MaxValue) |> int64 )
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (float Int64.MaxValue) |> int64 )
|> Array.countBy (fun x -> x / ONE_TENTH) // Categorize and count random numbers.
|> Array.map snd
@@ -17,7 +17,7 @@ printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
for i = 0 to 9 do
let r1 = int64 i * ONE_TENTH
let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
- printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {double count.[i] / 20000000.0,7:P2}"
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {float count.[i] / 20000000.0,7:P2}"
// The example displays output like the following:
// Range Count Pct.
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
index 2b13bef4018..e4df3e4e015 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
@@ -4,11 +4,11 @@ open System.Threading
type Example() =
[]
- static val mutable private previous : double
+ static val mutable private previous : float
[]
static val mutable private perThreadCtr : int
[]
- static val mutable private perThreadTotal : double
+ static val mutable private perThreadTotal : float
static let source = new CancellationTokenSource()
static let countdown = new CountdownEvent 1
From 05cd5d3d7213ac13b7cb8464f60a6bb168e14766 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Thu, 28 Oct 2021 22:03:21 -0700
Subject: [PATCH 08/14] fix file name
---
xml/System/Random.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index 3fac9a85487..48057be5728 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -652,7 +652,7 @@ The following example uses the parameterless constructor to instantiate three property is supplied as a the `maxValue` parameter.
From 445780a427975a212e1131109fd4035801aeb5e8 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Fri, 29 Oct 2021 18:58:30 -0700
Subject: [PATCH 09/14] minor adjustments
* fixed snippet number
* fixed float printing to be more consistent with .ToString()
---
.../VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs | 3 +--
.../VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs | 2 +-
.../fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs | 4 ++--
3 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
index f97a0064217..820312f62db 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
@@ -49,8 +49,7 @@ for _ = 1 to rows do
printfn ""
printfn $"""
-To demonstrate the proportional distribution,
-{runCount:N0} random
+To demonstrate the proportional distribution, {runCount:N0} random
integers and doubles are grouped into {distGroupCount} equal value ranges. This
is the count of values in each range:
"""
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
index 8637f3d32db..08e8f4ed2d2 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
@@ -4,7 +4,7 @@ open System
let rnd = Random()
for _ = 1 to 10 do
- printfn "%f" (rnd.NextDouble() - 1.0)
+ printfn "%O" (rnd.NextDouble() - 1.0)
// The example displays output like the following:
// -0.930412760437658
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
index 240528abcfe..427e407da8d 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
@@ -1,6 +1,6 @@
open System
-//
+//
[]
let ONE_TENTH = 922337203685477581L
@@ -32,4 +32,4 @@ for i = 0 to 9 do
// 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
// 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
// 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
-//
+//
From bc288883fefb908beda4b047f1f8d34faa6e1d6b Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Mon, 1 Nov 2021 16:48:00 -0700
Subject: [PATCH 10/14] Add modules and fsproj files
---
.../system.Random.Ctor/FS/RandomCtor.fsproj | 14 +++++++++
.../system.Random.Ctor/FS/ctor.fs | 2 ++
.../system.Random.Ctor/FS/ctor1.fs | 2 ++
.../system.Random.Ctor/FS/ctor4.fs | 2 ++
.../system.Random.Next/FS/Next2.fs | 2 ++
.../system.Random.Next/FS/RandomNext.fsproj | 16 ++++++++++
.../system.Random.Next/FS/next.fs | 2 ++
.../system.Random.Next/FS/next1.fs | 2 ++
.../system.Random.Next/FS/next3.fs | 2 ++
.../system.Random.Next/FS/next4.fs | 2 ++
.../system.Random.Sample/FS/sample.fs | 12 +++----
.../system.Random.Sample/FS/sample.fsproj | 12 +++++++
.../system.Random/fs/Random.fsproj | 31 +++++++++++++++++++
.../system.Random/fs/Random1.fs | 2 ++
.../system.Random/fs/Random2.fs | 2 ++
.../system.Random/fs/array1.fs | 2 ++
.../system.Random/fs/booleans1.fs | 2 ++
.../system.Random/fs/booleans2.fs | 2 ++
.../system.Random/fs/bytes1.fs | 2 ++
.../system.Random/fs/doublerange1.fs | 2 ++
.../system.Random/fs/doublerange2.fs | 2 ++
.../system.Random/fs/doublerange3.fs | 2 ++
.../system.Random/fs/long1.fs | 2 ++
.../system.Random/fs/nextbytes1.fs | 2 ++
.../system.Random/fs/nextdoubleex1.fs | 2 ++
.../system.Random/fs/nextex1.fs | 2 ++
.../system.Random/fs/range1.fs | 2 ++
.../system.Random/fs/range2.fs | 2 ++
.../system.Random/fs/same1.fs | 16 +++++-----
.../system.Random/fs/threadsafeex1.fs | 2 ++
.../system.Random/fs/threadsafeex2.fs | 2 ++
.../system.Random/fs/unique.fs | 2 ++
.../system.Random/fs/uniquearray1.fs | 2 ++
.../fs/nextdouble1.fsproj | 12 +++++++
34 files changed, 152 insertions(+), 15 deletions(-)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/RandomCtor.fsproj
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/RandomNext.fsproj
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fsproj
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random.fsproj
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fsproj
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/RandomCtor.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/RandomCtor.fsproj
new file mode 100644
index 00000000000..2cd65d379ed
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/RandomCtor.fsproj
@@ -0,0 +1,14 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
+
+
+
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
index cde1c280acc..6404c5be5bb 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
@@ -1,3 +1,5 @@
+module Ctor
+
//
// Example of the Random class constructors and Random.NextDouble()
// method.
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
index 9add40bcf3c..9871de3eb83 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
@@ -1,3 +1,5 @@
+module Ctor1
+
//
open System
open System.Threading
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
index 1e438519a5d..dee51bc7e71 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
@@ -1,3 +1,5 @@
+module Ctor4
+
//
open System
open System.Threading
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
index 00b18b70681..fd7e30a26bf 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
@@ -1,3 +1,5 @@
+module Next2
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/RandomNext.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/RandomNext.fsproj
new file mode 100644
index 00000000000..059a91524f7
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/RandomNext.fsproj
@@ -0,0 +1,16 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
index da81c55a94e..1f6e89b8f4b 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
@@ -1,3 +1,5 @@
+module Next
+
// Example of the Random.Next() methods.
open System
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
index a802626466e..f2d3fe4c2a1 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
@@ -1,3 +1,5 @@
+module Next1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
index ec804b445a1..683faddf4dc 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
@@ -1,3 +1,5 @@
+module Next3
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
index 65abb4c82b5..f614e1b4b7f 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next4.fs
@@ -1,3 +1,5 @@
+module Next4
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
index 820312f62db..f2d14fcf9b1 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fs
@@ -67,20 +67,20 @@ let intCounts =
let realCounts =
Array.init runCount (fun _ ->
- randObj.NextDouble() * float intGroupSize
+ randObj.NextDouble() * float distGroupCount
|> int )
|> Array.countBy id
|> Array.map snd
// Display the count of each group.
-for i = 1 to distGroupCount - 1 do
+for i = 0 to distGroupCount - 1 do
Console.WriteLine(
"{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}",
- (int)((float)i * intGroupSize),
- (int)((float)(i + 1) * intGroupSize - 1.0),
+ int(float i * intGroupSize),
+ int(float (i + 1) * intGroupSize - 1.0),
intCounts.[i],
- ((float)i) / (float)distGroupCount,
- ((float)(i + 1)) / (float)distGroupCount,
+ (float i) / float distGroupCount,
+ float (i + 1) / float distGroupCount,
realCounts.[i])
(*
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fsproj
new file mode 100644
index 00000000000..3839cad34f1
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Sample/FS/sample.fsproj
@@ -0,0 +1,12 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
+
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random.fsproj
new file mode 100644
index 00000000000..3ea45a97141
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random.fsproj
@@ -0,0 +1,31 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
index ca391e4b79d..e7c47242c49 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
@@ -1,3 +1,5 @@
+module Random1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
index e65c6c6f917..719030a3128 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
@@ -1,3 +1,5 @@
+module Random2
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
index e1d9186ad45..ca56497d573 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/array1.fs
@@ -1,3 +1,5 @@
+module Array1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
index 8d8b63f69aa..44a9bba0f75 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans1.fs
@@ -1,3 +1,5 @@
+module Booleans1
+
//
open System
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
index 0126e9feb37..e4e146c3a8d 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/booleans2.fs
@@ -1,3 +1,5 @@
+module Booleans2
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
index aecce4b42bf..2c3da10c0ef 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
@@ -1,3 +1,5 @@
+module Bytes1
+
//
open System
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
index 5b6def11e1d..57d0d9401a3 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
@@ -1,3 +1,5 @@
+module DoubleRange1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
index 08e8f4ed2d2..04c48ba953f 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange2.fs
@@ -1,3 +1,5 @@
+module DoubleRange2
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
index a63dca2f0a8..829f6d79b1e 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange3.fs
@@ -1,3 +1,5 @@
+module DoubleRange3
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
index 427e407da8d..90b86226dce 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/long1.fs
@@ -1,3 +1,5 @@
+module Long1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
index 23aeb5eb453..c94b7ed9baf 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
@@ -1,3 +1,5 @@
+module NextBytes1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
index 74586cf83fc..5e1fe90a370 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextdoubleex1.fs
@@ -1,3 +1,5 @@
+module NextDoubleEx1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
index 9c51119921c..9b650155718 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextex1.fs
@@ -1,3 +1,5 @@
+module NextEx1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
index 08b2a128612..13943083adb 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range1.fs
@@ -1,3 +1,5 @@
+module Range1
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
index aed0ec61bbd..903c82a4ff9 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
@@ -1,3 +1,5 @@
+module Range2
+
open System
//
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
index 6a0a75fa019..0d8e9045a16 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/same1.fs
@@ -1,3 +1,5 @@
+module Same1
+
//
open System
open System.IO
@@ -19,17 +21,13 @@ let displayNewRandomNumbers () =
for _ = 0 to 20 do
printfn $"{rnd.NextDouble()}"
-[]
-let main _ =
- let seed = 100100
- showRandomNumbers seed
- printfn ""
-
- persistSeed seed
+let seed = 100100
+showRandomNumbers seed
+printfn ""
- displayNewRandomNumbers ()
+persistSeed seed
- 0
+displayNewRandomNumbers ()
// The example displays output like the following:
// 0.500193602172748
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
index e4df3e4e015..81a4a67bcf1 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
@@ -1,3 +1,5 @@
+module ThreadSafeEx1
+
//
open System
open System.Threading
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
index 754c2084f92..addb5c02560 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
@@ -1,3 +1,5 @@
+module ThreadSafeEx2
+
//
open System
open System.Threading
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
index 7515f8ba08b..2a0d2c1466e 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/unique.fs
@@ -1,3 +1,5 @@
+module Unique
+
//
open System
open System.Threading
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
index d25be630563..7909a607a4b 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
@@ -1,3 +1,5 @@
+module UniqueArray1
+
//
open System
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fsproj
new file mode 100644
index 00000000000..56913f45d9c
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.random.nextdouble/fs/nextdouble1.fsproj
@@ -0,0 +1,12 @@
+
+
+
+ Exe
+ net5.0
+
+
+
+
+
+
+
From 411d838a1110d1603f1a15c77801a9902e8310c0 Mon Sep 17 00:00:00 2001
From: Albert Du <52804499+albert-du@users.noreply.github.com>
Date: Fri, 12 Nov 2021 14:41:01 -0800
Subject: [PATCH 11/14] Apply suggestions from code review
Co-authored-by: Chet Husk
---
.../system.Random.Ctor/FS/ctor.fs | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
index 6404c5be5bb..0fa5b5f28cc 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
@@ -19,7 +19,7 @@ let runIntNDoubleRandoms (randObj: Random) =
printfn ""
let fixedSeedRandoms seed =
- printfn "\nRandom numbers from a Random object with seed = %i:" seed
+ printfn $"\nRandom numbers from a Random object with seed = %i{seed}:"
let fixRand = Random seed
runIntNDoubleRandoms fixRand
@@ -33,10 +33,11 @@ let autoSeedRandoms () =
runIntNDoubleRandoms autoRand
-printfn "This example of the Random class constructors and Random.NextDouble()"
-printfn "generates the following output.\n"
-printfn "Create Random objects, and then generate and display six integers and"
-printfn "six doubles from each."
+printfn
+ """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."""
fixedSeedRandoms 123
fixedSeedRandoms 123
@@ -90,10 +91,7 @@ Random numbers from a Random object with an auto-generated seed:
let createEngineWithSameTimer () =
//
let randomEngines =
- Array.init 4 (fun i ->
- Operators.(>>>) DateTime.Now.Ticks i
- |> int
- |> Random )
+ [| for i in 0 .. 3 -> Random(int (DateTime.Now.Ticks >>> i)) |]
//
for i in randomEngines do
printfn "%i" (i.Next())
From f13fc9e092c1c8a9881e571cb74df73e755a1443 Mon Sep 17 00:00:00 2001
From: Albert Du <52804499+albert-du@users.noreply.github.com>
Date: Fri, 12 Nov 2021 14:56:39 -0800
Subject: [PATCH 12/14] Update Random.xml
Include information for F#
---
xml/System/Random.xml | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index 48057be5728..05c242b9a13 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -115,7 +115,7 @@ To avoid this problem, create a single object instead of mu
## The System.Random class and thread safety
Instead of instantiating individual objects, we recommend that you create a single instance to generate all the random numbers needed by your app. However, objects are not thread safe. If your app calls methods from multiple threads, you must use a synchronization object to ensure that only one thread can access the random number generator at a time. If you don't ensure that the object is accessed in a thread-safe way, calls to methods that return random numbers return 0.
- The following example uses the C# [lock Statement](/dotnet/csharp/language-reference/keywords/lock-statement) and the Visual Basic [SyncLock statement](/dotnet/visual-basic/language-reference/statements/synclock-statement) to ensure that a single random number generator is accessed by 11 threads in a thread-safe manner. Each thread generates 2 million random numbers, counts the number of random numbers generated and calculates their sum, and then updates the totals for all threads when it finishes executing.
+ The following example uses the C# [lock Statement](/dotnet/csharp/language-reference/keywords/lock-statement), the F# [lock function](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-operators.html#lock) and the Visual Basic [SyncLock statement](/dotnet/visual-basic/language-reference/statements/synclock-statement) to ensure that a single random number generator is accessed by 11 threads in a thread-safe manner. Each thread generates 2 million random numbers, counts the number of random numbers generated and calculates their sum, and then updates the totals for all threads when it finishes executing.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Random/cpp/threadsafeex1.cpp" id="Snippet3":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Random/cs/threadsafeex1.cs" id="Snippet3":::
@@ -126,7 +126,7 @@ To avoid this problem, create a single object instead of mu
- The attribute is used to define thread-local variables that track the total number of random numbers generated and their sum for each thread.
-- A lock (the `lock` statement in C# and the `SyncLock` statement in Visual Basic) protects access to the variables for the total count and sum of all random numbers generated on all threads.
+- A lock (the `lock` statement in C#, the `lock` function in F# and the `SyncLock` statement in Visual Basic) protects access to the variables for the total count and sum of all random numbers generated on all threads.
- A semaphore (the object) is used to ensure that the main thread blocks until all other threads complete execution.
@@ -309,7 +309,7 @@ Random.NextDouble() * (maxValue - minValue) + minValue
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs" id="Snippet9":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Random/vb/bytes1.vb" id="Snippet9":::
- The `NextBytes(Byte[], Byte, Byte)` method wraps a call to the method and specifies the minimum value and one greater than the maximum value (in this case, 0 and 101) that we want returned in the byte array. Because we are sure that the integer values returned by the method are within the range of the data type, we can safely cast them (in C#) or convert them (in Visual Basic) from integers to bytes.
+ The `NextBytes(Byte[], Byte, Byte)` method wraps a call to the method and specifies the minimum value and one greater than the maximum value (in this case, 0 and 101) that we want returned in the byte array. Because we are sure that the integer values returned by the method are within the range of the data type, we can safely cast them (in C# and F#) or convert them (in Visual Basic) from integers to bytes.
### Retrieve an element from an array or collection at random
From 11f38b6a95aa46be4762f381b2a1920e94200384 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Fri, 19 Nov 2021 18:17:18 -0800
Subject: [PATCH 13/14] improve snippet formatting
removed type annotations from array creation
replaced another "double" with "float"
normalized formatting
---
.../system.Random.Ctor/FS/ctor1.fs | 3 +-
.../system.Random.Ctor/FS/ctor4.fs | 3 +-
.../system.Random/fs/Random1.fs | 4 +-
.../system.Random/fs/Random2.fs | 2 +-
.../system.Random/fs/bytes1.fs | 4 +-
.../system.Random/fs/doublerange1.fs | 2 +-
.../system.Random/fs/nextbytes1.fs | 2 +-
.../system.Random/fs/range2.fs | 3 +-
.../system.Random/fs/threadsafeex1.fs | 5 +-
.../system.Random/fs/threadsafeex2.fs | 81 ++++++++++---------
.../system.Random/fs/uniquearray1.fs | 4 +-
11 files changed, 60 insertions(+), 53 deletions(-)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
index 9871de3eb83..183a96f3810 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
@@ -6,7 +6,7 @@ open System.Threading
let showRandomNumbers (rand: Random) =
printfn ""
- let values : byte [] = Array.zeroCreate 5
+ let values = Array.zeroCreate 5
rand.NextBytes values
for value in values do
printf "%5i" value
@@ -16,6 +16,7 @@ let rand1 = Random()
let rand2 = Random()
Thread.Sleep 2000
let rand3 = Random()
+
showRandomNumbers rand1
showRandomNumbers rand2
showRandomNumbers rand3
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
index dee51bc7e71..7f8301f8d0f 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
@@ -6,7 +6,7 @@ open System.Threading
let showRandomNumbers (rand: Random) =
printfn ""
- let values : byte [] = Array.zeroCreate 5
+ let values = Array.zeroCreate 5
rand.NextBytes values
for value in values do
printf "%5i" value
@@ -16,6 +16,7 @@ let rand1 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
let rand2 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
Thread.Sleep 20
let rand3 = Random(DateTime.Now.Ticks &&& 0x0000FFFFL |> int)
+
showRandomNumbers rand1
showRandomNumbers rand2
showRandomNumbers rand3
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
index e7c47242c49..0017b215d5b 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random1.fs
@@ -3,8 +3,8 @@ module Random1
open System
//
-let bytes1 : byte [] = Array.zeroCreate 100
-let bytes2 : byte [] = Array.zeroCreate 100
+let bytes1 = Array.zeroCreate 100
+let bytes2 = Array.zeroCreate 100
let rnd1 = Random()
let rnd2 = Random()
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
index 719030a3128..adc4e633249 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/Random2.fs
@@ -7,7 +7,7 @@ open System
let rand = Random()
// Generate and display 5 random byte (integer) values.
-let bytes : byte [] = Array.zeroCreate 5
+let bytes = Array.zeroCreate 5
rand.NextBytes bytes
printfn "Five random byte values:"
for byte in bytes do
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
index 2c3da10c0ef..9b605bc10ea 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/bytes1.fs
@@ -11,8 +11,8 @@ type Random2() =
bytes.[i] <- this.Next(int minValue, int maxValue) |> byte
let rnd = Random2()
-let bytes : byte [] = Array.zeroCreate 10000
-let total : int [] = Array.zeroCreate 101
+let bytes = Array.zeroCreate 10000
+let total = Array.zeroCreate 101
rnd.NextBytes(bytes, 0uy, 101uy)
// Calculate how many of each value we have.
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
index 57d0d9401a3..d4c9fe115ef 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/doublerange1.fs
@@ -10,7 +10,7 @@ let rnd = Random()
// Generate 20 million random integers.
let count =
- Array.init 20000000 (fun _ -> rnd.NextDouble() * (double Int64.MaxValue) )
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (float Int64.MaxValue) )
|> Array.countBy (fun x -> x / (float ONE_TENTH) |> int ) // Categorize into 10 groups and count them.
|> Array.map snd
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
index c94b7ed9baf..53520b2b3ce 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/nextbytes1.fs
@@ -4,7 +4,7 @@ open System
//
let rnd = Random()
-let bytes : Byte [] = Array.zeroCreate 20
+let bytes = Array.zeroCreate 20
rnd.NextBytes bytes
for i = 1 to bytes.Length do
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
index 903c82a4ff9..3f8a084972c 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/range2.fs
@@ -4,9 +4,8 @@ open System
//
let rnd = Random()
-
for i = 1 to 50 do
- printf "%3i " (rnd.Next(1000,10000))
+ printf "%3i " (rnd.Next(1000, 10000))
if i % 10 = 0 then printfn ""
// The example displays output like the following:
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
index 81a4a67bcf1..df8058ffdd9 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex1.fs
@@ -7,16 +7,19 @@ open System.Threading
type Example() =
[]
static val mutable private previous : float
+
[]
static val mutable private perThreadCtr : int
+
[]
static val mutable private perThreadTotal : float
static let source = new CancellationTokenSource()
- static let countdown = new CountdownEvent 1
+ static let countdown = new CountdownEvent(1)
static let randLock = obj ()
static let numericLock = obj ()
static let rand = Random()
+
let mutable totalValue = 0.0
let mutable totalCount = 0
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
index addb5c02560..aa9cd5bdee2 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/threadsafeex2.fs
@@ -16,44 +16,47 @@ type Example() =
let mutable totalCount = 0
member _.Execute() =
- use source = source // Dispose of the cancellationTokenSource when we're done with it.
+ use source = source // Dispose of the CancellationTokenSource when we're done with it.
let token = source.Token
- let tasks = [|
- for i = 0 to 10 do
- Task.Run(fun () ->
- let mutable previous = 0.0
- let mutable taskCtr = 0
- let mutable taskTotal = 0.0
- let mutable result = 0.0
-
- for _ = 1 to 2000000 do
- // Make sure there's no corruption of Random.
- token.ThrowIfCancellationRequested()
-
- lock randLock (fun () ->
- result <- rand.NextDouble() )
-
- // Check for corruption of Random instance.
- if result = previous && result = 0.0 then
- source.Cancel()
- else
- previous <- result
-
- taskCtr <- taskCtr + 1
- taskTotal <- taskTotal + result
-
- lock numericLock (fun () ->
- // Show result.
- printfn "Task %i finished execution." i
- printfn $"Random numbers generated: {taskCtr:N0}"
- printfn $"Sum of random numbers: {taskTotal:N2}"
- printfn $"Random number mean: {(taskTotal / float taskCtr):N4}\n"
-
- // Update overall totals.
- totalCount <- totalCount + taskCtr
- totalValue <- totalValue + taskTotal)
- , token) |]
- try
+
+ let tasks =
+ [| for i = 0 to 10 do
+ Task.Run(
+ (fun () ->
+ let mutable previous = 0.0
+ let mutable taskCtr = 0
+ let mutable taskTotal = 0.0
+ let mutable result = 0.0
+
+ for _ = 1 to 2000000 do
+ // Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ lock randLock (fun () -> result <- rand.NextDouble())
+
+ // Check for corruption of Random instance.
+ if result = previous && result = 0.0 then
+ source.Cancel()
+ else
+ previous <- result
+
+ taskCtr <- taskCtr + 1
+ taskTotal <- taskTotal + result
+
+ lock numericLock (fun () ->
+ // Show result.
+ printfn "Task %i finished execution." i
+ printfn $"Random numbers generated: {taskCtr:N0}"
+ printfn $"Sum of random numbers: {taskTotal:N2}"
+ printfn $"Random number mean: {(taskTotal / float taskCtr):N4}\n"
+
+ // Update overall totals.
+ totalCount <- totalCount + taskCtr
+ totalValue <- totalValue + taskTotal)),
+ token
+ ) |]
+
+ try
// Run tasks with F# Async.
Task.WhenAll tasks
|> Async.AwaitTask
@@ -65,11 +68,11 @@ type Example() =
with
| :? AggregateException as e ->
for inner in e.InnerExceptions do
- match inner with
+ match inner with
| :? TaskCanceledException as canc ->
if canc <> null then
printfn $"Task #{canc.Task.Id} cancelled"
- else
+ else
printfn $"Exception: {inner.GetType().Name}"
| _ -> ()
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
index 7909a607a4b..5cae6445e6d 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random/fs/uniquearray1.fs
@@ -17,8 +17,8 @@ type Face =
type Card = { Face: Face; Suit: Suit }
-let suits = [Clubs; Diamonds; Hearts; Spades]
-let faces = [Ace; Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten; Jack; Queen; King]
+let suits = [ Clubs; Diamonds; Hearts; Spades ]
+let faces = [ Ace; Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten; Jack; Queen; King ]
type Dealer() =
let rnd = Random()
From 5dcb992b9b57926f8393b9252cd0938e9e24f91b Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Sat, 20 Nov 2021 10:21:11 -0800
Subject: [PATCH 14/14] use string interpolation
---
.../system.Random.Next/FS/next.fs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
index 1f6e89b8f4b..268c8bb60c0 100644
--- a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
+++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
@@ -10,27 +10,27 @@ let noBoundsRandoms seed =
// Generate six random integers from 0 to int.MaxValue.
for _ = 1 to 6 do
- printf "%11i " (randObj.Next())
+ printf $"%11i{randObj.Next()} "
printfn ""
// Generate random numbers with an upper bound specified.
let upperBoundRandoms seed upper =
- printfn "\nRandom object, seed = %i, upper bound = %i" seed upper
+ printfn $"\nRandom object, seed = %i{seed}, upper bound = %i{upper}"
let randObj = Random seed
// Generate six random integers from 0 to the upper bound.
for _ = 1 to 6 do
- printf "%11i " (randObj.Next(upper))
+ printf $"%11i{randObj.Next upper} "
printfn ""
// Generate random numbers with both bounds specified.
let bothBoundRandoms seed lower upper =
- printfn "\nRandom object, seed = %i, lower = %i, upper = %i: " seed lower upper
+ printfn $"\nRandom object, seed = %i{seed}, lower = %i{lower}, upper = %i{upper}: "
let randObj = Random seed
// Generate six random integers from the lower to upper bounds.
for _ = 1 to 6 do
- printf "%11i " (randObj.Next(lower,upper))
+ printf $"%11i{randObj.Next(lower,upper)} "
printfn ""
printfn "This example of the Random.Next() methods\ngenerates the following.\n"