2 people like it.

Array shuffle

Shuffle an array

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

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

// shuffle an array (in-place)
let shuffle a =
    Array.iteri (fun i _ -> swap a i (rand.Next(i, Array.length a))) a
val rand : System.Random

Full name: Script.rand
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val swap : a:'a [] -> x:int -> y:int -> unit

Full name: Script.swap
val a : 'a []
val x : int
val y : int
val tmp : 'a
val shuffle : a:'a [] -> unit

Full name: Script.shuffle
module Array

from Microsoft.FSharp.Collections
val iteri : action:(int -> 'T -> unit) -> array:'T [] -> unit

Full name: Microsoft.FSharp.Collections.Array.iteri
val i : int
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
Next Version Raw view Test code New version

More information

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