Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active November 20, 2022 23:21
Show Gist options
  • Save aloisdg/e815f4b993780b8129e337eb2effdc5c to your computer and use it in GitHub Desktop.
Save aloisdg/e815f4b993780b8129e337eb2effdc5c to your computer and use it in GitHub Desktop.
Get a random element in a list in F#
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"
@aloisdg
Copy link
Author

aloisdg commented Mar 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment