jeroldhaas
7/17/2013 - 9:46 PM

Torture-testing Seq.distinct by intentionally taking more than the generator's distinct set size.

Torture-testing Seq.distinct by intentionally taking more than the generator's distinct set size.

// Revisions to original "GreedyDistinctTrap", removing the trap, plus adding Crypto RNG and StopWatch

open System
open System.Diagnostics

let randGen i =
    let r = new Random()
    r.Next()

let secRandGen i =
    let r = new System.Security.Cryptography.RNGCryptoServiceProvider()
    let arrayHack = [| 00uy |]
    r.GetBytes arrayHack
    int arrayHack.[0]


[<EntryPoint>]
let main argv =
    let sw = new Stopwatch()

    // System.Random
    let impossibleSeq =
        Seq.initInfinite (fun i -> randGen i)
        |> Seq.distinct
        |> Seq.take 10000
    sw.Reset |> ignore
    sw.Start |> ignore
    Seq.toList impossibleSeq
        |> List.map (fun x -> printfn "%A" x) |> ignore
    sw.Stop |> ignore
    let rndTime = sw.ElapsedMilliseconds

    // System.Security.Cryptography
    let cryptoSeq =
        Seq.initInfinite (fun i -> secRandGen i)
        |> Seq.distinct
        |> Seq.take 10000
    sw.Reset |> ignore
    sw.Start |> ignore
    Seq.toList cryptoSeq
     |> List.map (fun x -> printfn "%A" x) |> ignore
    sw.Stop |> ignore
    let cryptoTime = sw.ElapsedMilliseconds

    printfn "System.Random: %A" rndTime
    printfn "System.Security.Cryptography: %A" cryptoTime
    printfn "Press Enter: "
    Console.ReadLine() |> ignore
    0 // return an integer exit code
open System

let randGen low high =
    Seq.initInfinite( fun i -> (new Random()).Next(low, high))
    |> Seq.distinct

[<EntryPoint>]
let main argv = 
    let impossibleSeq = Seq.take 10000 (randGen 0 1000)
    Seq.map (fun x -> Console.WriteLine(x.ToString())) impossibleSeq
    Console.ReadLine() |> ignore
    0