mercredi 31 août 2016

F# Random elements from a set

I'm working on a project that requires me to write a function that selects a designated number of random elements from a set. Then map those elements to a variable for later comparison.

So in my scenario I have to select 5% of any given set.

let rec randomSet (a:Set<string>) =
let setLength = (a.Count / 100) * 5

let list = []
let rand = System.Random
if set.Length <> setLength then
    // some code will go here
    randomSet setLength eIDS
else
    set

^Please criticize my code, I've only being coding in F# for a week.

I've tried to do it recursively but I have a feeling that it's the wrong way to go. I have tried other methods but they use the .take function, and thus the returned collection is the same every time.

Any ideas? I'm not after 1 element from a set, I'm after 5% of any set that's thrown at it.

This is not the same question as this :How can I select a random value from a list using F#

If you think it is, please explain.




Aucun commentaire:

Enregistrer un commentaire