2 people like it.

Array shuffle

Shuffle an array

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let rand = new System.Random()

let swap x y (a: _ array) =
    let tmp = a.[x]
    a.[x] <- a.[y]
    a.[y] <- tmp

// shuffle an array (in-place)
let shuffle a = Array.iteri (fun i x -> x |> swap i (rand.Next(i, Array.length x)))
val rand : System.Random
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit + 1 overload
  member NextDouble : unit -> float

--------------------
System.Random() : System.Random
System.Random(Seed: int) : System.Random
val swap : x:int -> y:int -> a:'a array -> unit
val x : int
val y : int
val a : 'a array
type 'T array = 'T []
val tmp : 'a
val shuffle : a:'a -> ('b array [] -> unit)
val a : 'a
module Array

from Microsoft.FSharp.Collections
val iteri : action:(int -> 'T -> unit) -> array:'T [] -> unit
val i : int
val x : 'b array
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
val length : array:'T [] -> int

More information

Link:http://fssnip.net/L
Posted:2 years ago
Author:Josh DeGraw
Tags: array , shuffle , random