Skip to content
Prev Previous commit
Next Next commit
system.random.nextdouble snippet
  • Loading branch information
albert-du committed Oct 29, 2021
commit 875fc7ef585da8cda62fe135dd438ef3cb821697
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
open System

// <Snippet2>
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
// </Snippet2>
1 change: 1 addition & 0 deletions xml/System/Random.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:System.Random.NextDouble%2A> 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":::

]]></format>
Expand Down