Best way to generate random indices into an array?
up vote
3
down vote
favorite
Good afternoon. I have a set of values from which I'd like to draw a random subset.
My first thought was this:
let getRandomIndices size count =
if size >= count then
let r = System.Random()
r.GetValues(0,size) |> Seq.take count |> Seq.toList
else
[0..size-1]
However, r.GetValues(0,size) may generate the same value multiple times. How can I get distinct values? My first thought is to repeatedly store indexes into a set until the set holds the desired number of elements? But this seems too procedural/not-functional enough? Is there a better way?
Or should I start with [0..size-1] and remove random elements from it until it holds the desired number indices?
I'm not really looking for the most efficient approach, but the most functional one. I am struggling to better grok the functional mindset.
functional-programming f# fscheck
add a comment |
up vote
3
down vote
favorite
Good afternoon. I have a set of values from which I'd like to draw a random subset.
My first thought was this:
let getRandomIndices size count =
if size >= count then
let r = System.Random()
r.GetValues(0,size) |> Seq.take count |> Seq.toList
else
[0..size-1]
However, r.GetValues(0,size) may generate the same value multiple times. How can I get distinct values? My first thought is to repeatedly store indexes into a set until the set holds the desired number of elements? But this seems too procedural/not-functional enough? Is there a better way?
Or should I start with [0..size-1] and remove random elements from it until it holds the desired number indices?
I'm not really looking for the most efficient approach, but the most functional one. I am struggling to better grok the functional mindset.
functional-programming f# fscheck
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Good afternoon. I have a set of values from which I'd like to draw a random subset.
My first thought was this:
let getRandomIndices size count =
if size >= count then
let r = System.Random()
r.GetValues(0,size) |> Seq.take count |> Seq.toList
else
[0..size-1]
However, r.GetValues(0,size) may generate the same value multiple times. How can I get distinct values? My first thought is to repeatedly store indexes into a set until the set holds the desired number of elements? But this seems too procedural/not-functional enough? Is there a better way?
Or should I start with [0..size-1] and remove random elements from it until it holds the desired number indices?
I'm not really looking for the most efficient approach, but the most functional one. I am struggling to better grok the functional mindset.
functional-programming f# fscheck
Good afternoon. I have a set of values from which I'd like to draw a random subset.
My first thought was this:
let getRandomIndices size count =
if size >= count then
let r = System.Random()
r.GetValues(0,size) |> Seq.take count |> Seq.toList
else
[0..size-1]
However, r.GetValues(0,size) may generate the same value multiple times. How can I get distinct values? My first thought is to repeatedly store indexes into a set until the set holds the desired number of elements? But this seems too procedural/not-functional enough? Is there a better way?
Or should I start with [0..size-1] and remove random elements from it until it holds the desired number indices?
I'm not really looking for the most efficient approach, but the most functional one. I am struggling to better grok the functional mindset.
functional-programming f# fscheck
functional-programming f# fscheck
asked Nov 9 at 18:59
StevePoling
9710
9710
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
If you sort a list of all the indices randomly, you can just take the first count
number of elements in the list.
let getRandomIndices size count =
if size >= count then
let r = System.Random()
[0..size-1] |> List.sortBy (fun _ -> r.Next()) |> List.take count
else
[0..size-1]
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
If you sort a list of all the indices randomly, you can just take the first count
number of elements in the list.
let getRandomIndices size count =
if size >= count then
let r = System.Random()
[0..size-1] |> List.sortBy (fun _ -> r.Next()) |> List.take count
else
[0..size-1]
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
add a comment |
up vote
3
down vote
accepted
If you sort a list of all the indices randomly, you can just take the first count
number of elements in the list.
let getRandomIndices size count =
if size >= count then
let r = System.Random()
[0..size-1] |> List.sortBy (fun _ -> r.Next()) |> List.take count
else
[0..size-1]
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
If you sort a list of all the indices randomly, you can just take the first count
number of elements in the list.
let getRandomIndices size count =
if size >= count then
let r = System.Random()
[0..size-1] |> List.sortBy (fun _ -> r.Next()) |> List.take count
else
[0..size-1]
If you sort a list of all the indices randomly, you can just take the first count
number of elements in the list.
let getRandomIndices size count =
if size >= count then
let r = System.Random()
[0..size-1] |> List.sortBy (fun _ -> r.Next()) |> List.take count
else
[0..size-1]
answered Nov 9 at 19:06
Ringil
3,28221025
3,28221025
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
add a comment |
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
Beauty! That's the sort of thinking I need to be doing.
– StevePoling
Nov 9 at 19:11
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231807%2fbest-way-to-generate-random-indices-into-an-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown