Last active
November 20, 2022 23:21
-
-
Save aloisdg/e815f4b993780b8129e337eb2effdc5c to your computer and use it in GitHub Desktop.
Get a random element in a list in F#
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
type System.Random with | |
/// Generates an infinite sequence of random numbers within the given range. | |
member this.GetValues(minValue, maxValue) = | |
Seq.initInfinite (fun _ -> this.Next(minValue, maxValue)) | |
member this.GetItems(items:_[]) = | |
Seq.initInfinite (fun _ -> items.[this.Next(0, items.Length - 1)]) | |
let r = System.Random() | |
let nums = r.GetValues(1, 1000) |> Seq.take 10 | |
nums |> printfn "%A" | |
let nums2 = r.GetItems([|"titi1";"titi2";"titi3";"titi4";"tit5";"titi6";"tati1";"tati2";"tati3";"tati4";"tati5";"tati6"|]) |> Seq.take 1 | |
nums2 |> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try it online!