Skip to content

Instantly share code, notes, and snippets.

@jeroldhaas
Last active December 19, 2015 22:08
Show Gist options
  • Save jeroldhaas/6024834 to your computer and use it in GitHub Desktop.
Save jeroldhaas/6024834 to your computer and use it in GitHub Desktop.
Torture-testing Seq.distinct by intentionally taking more than the generator's distinct set size.
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
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment