-
Notifications
You must be signed in to change notification settings - Fork 1.6k
F# Snippets for System.Random #7335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BillWagner
merged 14 commits into
dotnet:main
from
albert-du:system-random-fsharp-snippets
Dec 10, 2021
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
51893c0
Add F# Snippets for System.Random
albert-du 1acd577
rename file
albert-du e67b8b9
system.Random.Ctor snippets
albert-du f95e3c8
system.Random.Next snippets
albert-du 875fc7e
system.random.nextdouble snippet
albert-du 5ae5a0f
system.Random.Sample snippet
albert-du 1790a92
double -> float
albert-du 05cd5d3
fix file name
albert-du 445780a
minor adjustments
albert-du bc28888
Add modules and fsproj files
albert-du 411d838
Apply suggestions from code review
albert-du f13fc9e
Update Random.xml
albert-du 11f38b6
improve snippet formatting
albert-du 5dcb992
use string interpolation
albert-du File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/RandomCtor.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="ctor.fs" /> | ||
| <Compile Include="ctor1.fs" /> | ||
| <Compile Include="ctor4.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
97 changes: 97 additions & 0 deletions
97
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| module Ctor | ||
|
|
||
| //<Snippet1> | ||
| // 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() | ||
| generates the following output. | ||
| Create Random objects, and then generate and display six integers and | ||
| 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 | ||
| *) | ||
| //</Snippet1> | ||
|
|
||
| // Code added to show how to initialize Random objects with the | ||
| // same timer value that will produce unique random number sequences. | ||
| let createEngineWithSameTimer () = | ||
| // <Snippet3> | ||
| let randomEngines = | ||
| [| for i in 0 .. 3 -> Random(int (DateTime.Now.Ticks >>> i)) |] | ||
| // </Snippet3> | ||
| for i in randomEngines do | ||
| printfn "%i" (i.Next()) |
30 changes: 30 additions & 0 deletions
30
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module Ctor1 | ||
|
|
||
| // <Snippet2> | ||
| open System | ||
| open System.Threading | ||
|
|
||
| let showRandomNumbers (rand: Random) = | ||
| printfn "" | ||
| let values = 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 | ||
| // </Snippet2> |
30 changes: 30 additions & 0 deletions
30
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Ctor/FS/ctor4.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module Ctor4 | ||
|
|
||
| // <Snippet4> | ||
| open System | ||
| open System.Threading | ||
|
|
||
| let showRandomNumbers (rand: Random) = | ||
| printfn "" | ||
| let values = 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 | ||
| // </Snippet4> |
41 changes: 41 additions & 0 deletions
41
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/Next2.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| module Next2 | ||
|
|
||
| open System | ||
|
|
||
| //<Snippet2> | ||
| 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 | ||
| // </Snippet2> |
16 changes: 16 additions & 0 deletions
16
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/RandomNext.fsproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="next.fs" /> | ||
| <Compile Include="next1.fs" /> | ||
| <Compile Include="Next2.fs" /> | ||
| <Compile Include="next3.fs" /> | ||
| <Compile Include="next4.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
89 changes: 89 additions & 0 deletions
89
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| module Next | ||
|
|
||
| // Example of the Random.Next() methods. | ||
| open System | ||
|
|
||
| //<Snippet1> | ||
| 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 | ||
albert-du marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
| *) | ||
| //</Snippet1> | ||
30 changes: 30 additions & 0 deletions
30
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next1.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module Next1 | ||
|
|
||
| open System | ||
|
|
||
| // <Snippet3> | ||
| 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 | ||
| // </Snippet3> |
26 changes: 26 additions & 0 deletions
26
samples/snippets/fsharp/VS_Snippets_CLR_System/system.Random.Next/FS/next3.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| module Next3 | ||
|
|
||
| open System | ||
|
|
||
| // <Snippet5> | ||
| 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 | ||
| // </Snippet5> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.