1 people like it.

Array shuffle

Shuffle an array

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let rand = new System.Random()
let swap x y (a: 'a []) =
    let tmp = a.[x]
    a.[x] <- a.[y]
    a.[y] <- tmp
// shuffle an array (in-place)
let shuffle a =
    Array.iteri (fun i _ -> a |> swap i (rand.Next(i, Array.length a)))
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 [] -> unit
val x : int
val y : int
val a : 'a []
val tmp : 'a
val shuffle : a:'a [] -> ('b [] -> unit)
module Array

from Microsoft.FSharp.Collections
val iteri : action:(int -> 'T -> unit) -> array:'T [] -> unit
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

More information

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