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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 18:59









      StevePoling

      9710




      9710






















          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]





          share|improve this answer




















          • Beauty! That's the sort of thinking I need to be doing.
            – StevePoling
            Nov 9 at 19:11










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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]





          share|improve this answer




















          • Beauty! That's the sort of thinking I need to be doing.
            – StevePoling
            Nov 9 at 19:11














          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]





          share|improve this answer




















          • Beauty! That's the sort of thinking I need to be doing.
            – StevePoling
            Nov 9 at 19:11












          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]





          share|improve this answer












          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]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo