1 people like it.

Shuffled Array

String.Join (" ", [| "This" ; "is"; "a" ; "way" ; "to" ; "generate" ; "a" ; "shuffled" ; "array" ; "with" ; "given" ; "elements" ; "from" ; "an" ; "(ordered)" ; "array." |]) |> printf "\n >> %A\n"

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
open System
    
let Swap (indexA : int) (indexB : int) (input : 'T[]) =
        
        let indexT : 'T = input.[indexA]
        input.[indexA] <- input.[indexB]
        input.[indexB] <- indexT 
    
        input
    
let rec Chaos (input : 'T[]) : 'T[] =
        
        let mutable result : 'T[] = input
        let rnd : Random = new Random()
    
        for i in 0 .. input.Length - 1 do 
            
            let j : int = rnd.Next ( 0 , input.Length - 1)
            
            result <- Swap i j input

        result

//  Demo:
printf "\n  >> %A -> %A" ([|0 .. 9|]) ([|0 .. 9|] |> Chaos)
namespace System
val Swap : indexA:int -> indexB:int -> input:'T [] -> 'T []

Full name: Script.Swap
val indexA : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val indexB : int
val input : 'T []
val indexT : 'T
val Chaos : input:'T [] -> 'T []

Full name: Script.Chaos
val mutable result : 'T []
val rnd : Random
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

--------------------
Random() : unit
Random(Seed: int) : unit
val i : int32
property Array.Length: int
val j : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7XC
Posted:4 years ago
Author:you
Tags: array , shuffle