Randomly distribute numbers to lists











up vote
4
down vote

favorite












I have 4 empty lists. I would like to randomly distribute numbers from 1 to 10 to these 4 empty lists. How can I add integers to lists randomly?



For example:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];


1->m , 2->n , 3->p , 4->m , 5->r , 6->r, 7->n , 8->m, 9->p , 10->n (Randomly distributed)



Here is the code I tried:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];
s = RandomChoice[{m, n, p, r}, 4];
Do[listm = AppendTo[s, listz[i]];, {i, 1, 10}]
listm


Wanted Solution:(Randomly distributed)



m = {1, 5, 8}
n = {4, 6, 9}
p = {7, 10}
r = {2, 3}









share|improve this question







New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why not just generate four random lists in the first place?
    – gothicVI
    Nov 4 at 20:00















up vote
4
down vote

favorite












I have 4 empty lists. I would like to randomly distribute numbers from 1 to 10 to these 4 empty lists. How can I add integers to lists randomly?



For example:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];


1->m , 2->n , 3->p , 4->m , 5->r , 6->r, 7->n , 8->m, 9->p , 10->n (Randomly distributed)



Here is the code I tried:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];
s = RandomChoice[{m, n, p, r}, 4];
Do[listm = AppendTo[s, listz[i]];, {i, 1, 10}]
listm


Wanted Solution:(Randomly distributed)



m = {1, 5, 8}
n = {4, 6, 9}
p = {7, 10}
r = {2, 3}









share|improve this question







New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Why not just generate four random lists in the first place?
    – gothicVI
    Nov 4 at 20:00













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have 4 empty lists. I would like to randomly distribute numbers from 1 to 10 to these 4 empty lists. How can I add integers to lists randomly?



For example:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];


1->m , 2->n , 3->p , 4->m , 5->r , 6->r, 7->n , 8->m, 9->p , 10->n (Randomly distributed)



Here is the code I tried:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];
s = RandomChoice[{m, n, p, r}, 4];
Do[listm = AppendTo[s, listz[i]];, {i, 1, 10}]
listm


Wanted Solution:(Randomly distributed)



m = {1, 5, 8}
n = {4, 6, 9}
p = {7, 10}
r = {2, 3}









share|improve this question







New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have 4 empty lists. I would like to randomly distribute numbers from 1 to 10 to these 4 empty lists. How can I add integers to lists randomly?



For example:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];


1->m , 2->n , 3->p , 4->m , 5->r , 6->r, 7->n , 8->m, 9->p , 10->n (Randomly distributed)



Here is the code I tried:



m = {};
n = {};
p = {};
r = {};
listz = Range[10];
s = RandomChoice[{m, n, p, r}, 4];
Do[listm = AppendTo[s, listz[i]];, {i, 1, 10}]
listm


Wanted Solution:(Randomly distributed)



m = {1, 5, 8}
n = {4, 6, 9}
p = {7, 10}
r = {2, 3}






list-manipulation random education






share|improve this question







New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 4 at 19:58









ithilquessirr

473




473




New contributor




ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






ithilquessirr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Why not just generate four random lists in the first place?
    – gothicVI
    Nov 4 at 20:00


















  • Why not just generate four random lists in the first place?
    – gothicVI
    Nov 4 at 20:00
















Why not just generate four random lists in the first place?
– gothicVI
Nov 4 at 20:00




Why not just generate four random lists in the first place?
– gothicVI
Nov 4 at 20:00










6 Answers
6






active

oldest

votes

















up vote
4
down vote



accepted










on Mathematica 11.3



(R=RandomSample)[Range@10,10]~TakeList~R@RandomChoice[Select[IntegerPartitions@10,Length@#==4&]]    





Also I tried to fix your code



a = {};
b = {};
c = {};
d = {};
Listz = {a, b, c, d};
Do[t=RandomChoice@Range@4;Listz[[t]]=AppendTo[Listz[[t]],i],{i,1,10}]
Listz





share|improve this answer























  • Have you tried that with a list of 100 elements?
    – Henrik Schumacher
    Nov 4 at 21:18










  • how about 10^100?
    – J42161217
    Nov 4 at 21:20






  • 1




    @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
    – Okkes Dulgerci
    Nov 4 at 21:23












  • @OkkesDulgerci fixed!
    – J42161217
    Nov 4 at 21:25




















up vote
2
down vote













n = 100;
m = 4;
list = Range[n];
s = RandomChoice[Range[m], n];

SparseArray[Transpose[{s, list}] -> _, {m, n}]["AdjacencyLists"]


Or slower, but easier to understand:



Lookup[GroupBy[Transpose[{s, list}], First -> Last], Range[m]]





share|improve this answer























  • Is this supposed to spew errors in 11.3?
    – user6014
    Nov 4 at 20:06










  • Ah, sorry. Please try again.
    – Henrik Schumacher
    Nov 4 at 20:10










  • Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
    – ithilquessirr
    Nov 4 at 20:19










  • Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
    – Henrik Schumacher
    Nov 4 at 20:52


















up vote
2
down vote













Why not use MultinomialDistribution of a random sample? For example:



rand[n_, s_] := TakeList[
RandomSample[Range[n]],
RandomVariate[MultinomialDistribution[n, ConstantArray[1/s, s]]]
]

rand[10, 4]



{{4, 6, 1}, {2}, {5, 9, 8, 7, 3}, {10}}







share|improve this answer




























    up vote
    2
    down vote













    listz = CharacterRange["A", "K"];
    k = 4;

    SeedRandom[1]
    rc = RandomChoice[Range @ k, Length @ listz];
    {m, n, p, r} = Pick[listz, rc, #]& /@ Range[k]



    {{"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}, {}, {"A", "J"}}




    Also



    Lookup[GroupBy[Transpose[{rc, listz}], First -> Last], #, {}]& /@ Range[k] 
    AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


    both give the same result. Also, using Carl Woll's GatherByList:



    RandomSample[PadRight[GatherByList[listz, rc], {k, Automatic}, {}]]



    {{"A", "J"}, {}, {"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}}




    Update: To get k non-empty sublists:



    SeedRandom[1]
    rc = Module[{r}, While[Length[Counts[r = RandomChoice[Range@k, Length @ listz]]] < k]; r];

    Pick[listz , rc, # ] & /@ Range[k]
    Lookup[GroupBy[Transpose[{rc , listz}], First -> Last], #, {}] & /@ Range[k]
    AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


    all give




    {{"A", "B", "C", "E", "H", "I"}, {"F"}, {"D", "G"}, {"J", "K"}}




    GatherByList[listz, rc] 



    {{"A", "B", "C", "E", "H", "I"}, {"D", "G"}, {"F"}, {"J", "K"}}




    Update 2:



    You can also use the function RandomKSetPartition from the Combinatorica package:



    Quiet @ Needs["Combinatorica`"]
    SeedRandom[1]
    RandomKSetPartition[listz, 4]



    {{"A", "D", "H"}, {"B"}, {"C", "F", "J", "K"}, {"E", "G", "I"}}







    share|improve this answer






























      up vote
      1
      down vote













      Edit: I realized that my previous solution doesn't produce the same length partitions. Here is the desired solution.



      s = RandomSample[Range@10, 10];
      t = RandomSample@RandomChoice@IntegerPartitions[10, {4}]
      {m, n, p, r} = TakeList[s, t]

      (*or {m, n, p, r} = FoldPairList[TakeDrop, s, t]*)



      {{1, 5, 10}, {3, 7}, {2, 9, 6, 8}, {4}}




      Original answer:



      s = RandomSample[Range@10, 10];
      t = RandomSample[Range@4, 4];
      {m, n, p, r} = FoldPairList[TakeDrop, s, t]



      {{6, 2}, {9}, {5, 1, 7}, {10, 3, 8, 4}}







      share|improve this answer






























        up vote
        0
        down vote













        A fast and short way is to utilize Pick. For this, we will need two commands:



        ClearAll[distribute];
        distribute[list_, partNum_] := RandomInteger[partNum - 1, Length@list]


        creates a number sequence which will assign each element to a list. And we do this with



        ClearAll[splitList];
        splitList[list_, partitionNumber_] := With[
        {selection = distribute[list, partitionNumber]},
        Table[Pick[list, selection, i], {i, 0, partitionNumber - 1}]
        ]


        That's it. The code is fast enough; on my laptop, it takes less than half a second to split a list of $10^7$ elements into 3 lists:



        splitList[Range[10^7], 3] // RepeatedTiming
        (* {0.42,...} *)


        It becomes slower if the number of list increases, as it is not optimized in that direction. Still, it should be fast enough, for example:



        splitList[Range[10^7], 10^3] // RepeatedTiming
        (* {8.3,...} *)





        share|improve this answer





















          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "387"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185296%2frandomly-distribute-numbers-to-lists%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          on Mathematica 11.3



          (R=RandomSample)[Range@10,10]~TakeList~R@RandomChoice[Select[IntegerPartitions@10,Length@#==4&]]    





          Also I tried to fix your code



          a = {};
          b = {};
          c = {};
          d = {};
          Listz = {a, b, c, d};
          Do[t=RandomChoice@Range@4;Listz[[t]]=AppendTo[Listz[[t]],i],{i,1,10}]
          Listz





          share|improve this answer























          • Have you tried that with a list of 100 elements?
            – Henrik Schumacher
            Nov 4 at 21:18










          • how about 10^100?
            – J42161217
            Nov 4 at 21:20






          • 1




            @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
            – Okkes Dulgerci
            Nov 4 at 21:23












          • @OkkesDulgerci fixed!
            – J42161217
            Nov 4 at 21:25

















          up vote
          4
          down vote



          accepted










          on Mathematica 11.3



          (R=RandomSample)[Range@10,10]~TakeList~R@RandomChoice[Select[IntegerPartitions@10,Length@#==4&]]    





          Also I tried to fix your code



          a = {};
          b = {};
          c = {};
          d = {};
          Listz = {a, b, c, d};
          Do[t=RandomChoice@Range@4;Listz[[t]]=AppendTo[Listz[[t]],i],{i,1,10}]
          Listz





          share|improve this answer























          • Have you tried that with a list of 100 elements?
            – Henrik Schumacher
            Nov 4 at 21:18










          • how about 10^100?
            – J42161217
            Nov 4 at 21:20






          • 1




            @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
            – Okkes Dulgerci
            Nov 4 at 21:23












          • @OkkesDulgerci fixed!
            – J42161217
            Nov 4 at 21:25















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          on Mathematica 11.3



          (R=RandomSample)[Range@10,10]~TakeList~R@RandomChoice[Select[IntegerPartitions@10,Length@#==4&]]    





          Also I tried to fix your code



          a = {};
          b = {};
          c = {};
          d = {};
          Listz = {a, b, c, d};
          Do[t=RandomChoice@Range@4;Listz[[t]]=AppendTo[Listz[[t]],i],{i,1,10}]
          Listz





          share|improve this answer














          on Mathematica 11.3



          (R=RandomSample)[Range@10,10]~TakeList~R@RandomChoice[Select[IntegerPartitions@10,Length@#==4&]]    





          Also I tried to fix your code



          a = {};
          b = {};
          c = {};
          d = {};
          Listz = {a, b, c, d};
          Do[t=RandomChoice@Range@4;Listz[[t]]=AppendTo[Listz[[t]],i],{i,1,10}]
          Listz






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 4 at 21:45

























          answered Nov 4 at 20:49









          J42161217

          2,637218




          2,637218












          • Have you tried that with a list of 100 elements?
            – Henrik Schumacher
            Nov 4 at 21:18










          • how about 10^100?
            – J42161217
            Nov 4 at 21:20






          • 1




            @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
            – Okkes Dulgerci
            Nov 4 at 21:23












          • @OkkesDulgerci fixed!
            – J42161217
            Nov 4 at 21:25




















          • Have you tried that with a list of 100 elements?
            – Henrik Schumacher
            Nov 4 at 21:18










          • how about 10^100?
            – J42161217
            Nov 4 at 21:20






          • 1




            @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
            – Okkes Dulgerci
            Nov 4 at 21:23












          • @OkkesDulgerci fixed!
            – J42161217
            Nov 4 at 21:25


















          Have you tried that with a list of 100 elements?
          – Henrik Schumacher
          Nov 4 at 21:18




          Have you tried that with a list of 100 elements?
          – Henrik Schumacher
          Nov 4 at 21:18












          how about 10^100?
          – J42161217
          Nov 4 at 21:20




          how about 10^100?
          – J42161217
          Nov 4 at 21:20




          1




          1




          @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
          – Okkes Dulgerci
          Nov 4 at 21:23






          @J42161217 Because Select[IntegerPartitions@10, Length@# == 4 &] sorted, you cannot get Lenght=1or 2 set for m.
          – Okkes Dulgerci
          Nov 4 at 21:23














          @OkkesDulgerci fixed!
          – J42161217
          Nov 4 at 21:25






          @OkkesDulgerci fixed!
          – J42161217
          Nov 4 at 21:25












          up vote
          2
          down vote













          n = 100;
          m = 4;
          list = Range[n];
          s = RandomChoice[Range[m], n];

          SparseArray[Transpose[{s, list}] -> _, {m, n}]["AdjacencyLists"]


          Or slower, but easier to understand:



          Lookup[GroupBy[Transpose[{s, list}], First -> Last], Range[m]]





          share|improve this answer























          • Is this supposed to spew errors in 11.3?
            – user6014
            Nov 4 at 20:06










          • Ah, sorry. Please try again.
            – Henrik Schumacher
            Nov 4 at 20:10










          • Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
            – ithilquessirr
            Nov 4 at 20:19










          • Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
            – Henrik Schumacher
            Nov 4 at 20:52















          up vote
          2
          down vote













          n = 100;
          m = 4;
          list = Range[n];
          s = RandomChoice[Range[m], n];

          SparseArray[Transpose[{s, list}] -> _, {m, n}]["AdjacencyLists"]


          Or slower, but easier to understand:



          Lookup[GroupBy[Transpose[{s, list}], First -> Last], Range[m]]





          share|improve this answer























          • Is this supposed to spew errors in 11.3?
            – user6014
            Nov 4 at 20:06










          • Ah, sorry. Please try again.
            – Henrik Schumacher
            Nov 4 at 20:10










          • Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
            – ithilquessirr
            Nov 4 at 20:19










          • Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
            – Henrik Schumacher
            Nov 4 at 20:52













          up vote
          2
          down vote










          up vote
          2
          down vote









          n = 100;
          m = 4;
          list = Range[n];
          s = RandomChoice[Range[m], n];

          SparseArray[Transpose[{s, list}] -> _, {m, n}]["AdjacencyLists"]


          Or slower, but easier to understand:



          Lookup[GroupBy[Transpose[{s, list}], First -> Last], Range[m]]





          share|improve this answer














          n = 100;
          m = 4;
          list = Range[n];
          s = RandomChoice[Range[m], n];

          SparseArray[Transpose[{s, list}] -> _, {m, n}]["AdjacencyLists"]


          Or slower, but easier to understand:



          Lookup[GroupBy[Transpose[{s, list}], First -> Last], Range[m]]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 4 at 21:30

























          answered Nov 4 at 20:01









          Henrik Schumacher

          44.2k263129




          44.2k263129












          • Is this supposed to spew errors in 11.3?
            – user6014
            Nov 4 at 20:06










          • Ah, sorry. Please try again.
            – Henrik Schumacher
            Nov 4 at 20:10










          • Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
            – ithilquessirr
            Nov 4 at 20:19










          • Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
            – Henrik Schumacher
            Nov 4 at 20:52


















          • Is this supposed to spew errors in 11.3?
            – user6014
            Nov 4 at 20:06










          • Ah, sorry. Please try again.
            – Henrik Schumacher
            Nov 4 at 20:10










          • Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
            – ithilquessirr
            Nov 4 at 20:19










          • Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
            – Henrik Schumacher
            Nov 4 at 20:52
















          Is this supposed to spew errors in 11.3?
          – user6014
          Nov 4 at 20:06




          Is this supposed to spew errors in 11.3?
          – user6014
          Nov 4 at 20:06












          Ah, sorry. Please try again.
          – Henrik Schumacher
          Nov 4 at 20:10




          Ah, sorry. Please try again.
          – Henrik Schumacher
          Nov 4 at 20:10












          Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
          – ithilquessirr
          Nov 4 at 20:19




          Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
          – ithilquessirr
          Nov 4 at 20:19












          Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
          – Henrik Schumacher
          Nov 4 at 20:52




          Well, you might inspect MatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]] to find that out.
          – Henrik Schumacher
          Nov 4 at 20:52










          up vote
          2
          down vote













          Why not use MultinomialDistribution of a random sample? For example:



          rand[n_, s_] := TakeList[
          RandomSample[Range[n]],
          RandomVariate[MultinomialDistribution[n, ConstantArray[1/s, s]]]
          ]

          rand[10, 4]



          {{4, 6, 1}, {2}, {5, 9, 8, 7, 3}, {10}}







          share|improve this answer

























            up vote
            2
            down vote













            Why not use MultinomialDistribution of a random sample? For example:



            rand[n_, s_] := TakeList[
            RandomSample[Range[n]],
            RandomVariate[MultinomialDistribution[n, ConstantArray[1/s, s]]]
            ]

            rand[10, 4]



            {{4, 6, 1}, {2}, {5, 9, 8, 7, 3}, {10}}







            share|improve this answer























              up vote
              2
              down vote










              up vote
              2
              down vote









              Why not use MultinomialDistribution of a random sample? For example:



              rand[n_, s_] := TakeList[
              RandomSample[Range[n]],
              RandomVariate[MultinomialDistribution[n, ConstantArray[1/s, s]]]
              ]

              rand[10, 4]



              {{4, 6, 1}, {2}, {5, 9, 8, 7, 3}, {10}}







              share|improve this answer












              Why not use MultinomialDistribution of a random sample? For example:



              rand[n_, s_] := TakeList[
              RandomSample[Range[n]],
              RandomVariate[MultinomialDistribution[n, ConstantArray[1/s, s]]]
              ]

              rand[10, 4]



              {{4, 6, 1}, {2}, {5, 9, 8, 7, 3}, {10}}








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 5 at 16:04









              Carl Woll

              64.3k283167




              64.3k283167






















                  up vote
                  2
                  down vote













                  listz = CharacterRange["A", "K"];
                  k = 4;

                  SeedRandom[1]
                  rc = RandomChoice[Range @ k, Length @ listz];
                  {m, n, p, r} = Pick[listz, rc, #]& /@ Range[k]



                  {{"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}, {}, {"A", "J"}}




                  Also



                  Lookup[GroupBy[Transpose[{rc, listz}], First -> Last], #, {}]& /@ Range[k] 
                  AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                  both give the same result. Also, using Carl Woll's GatherByList:



                  RandomSample[PadRight[GatherByList[listz, rc], {k, Automatic}, {}]]



                  {{"A", "J"}, {}, {"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}}




                  Update: To get k non-empty sublists:



                  SeedRandom[1]
                  rc = Module[{r}, While[Length[Counts[r = RandomChoice[Range@k, Length @ listz]]] < k]; r];

                  Pick[listz , rc, # ] & /@ Range[k]
                  Lookup[GroupBy[Transpose[{rc , listz}], First -> Last], #, {}] & /@ Range[k]
                  AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                  all give




                  {{"A", "B", "C", "E", "H", "I"}, {"F"}, {"D", "G"}, {"J", "K"}}




                  GatherByList[listz, rc] 



                  {{"A", "B", "C", "E", "H", "I"}, {"D", "G"}, {"F"}, {"J", "K"}}




                  Update 2:



                  You can also use the function RandomKSetPartition from the Combinatorica package:



                  Quiet @ Needs["Combinatorica`"]
                  SeedRandom[1]
                  RandomKSetPartition[listz, 4]



                  {{"A", "D", "H"}, {"B"}, {"C", "F", "J", "K"}, {"E", "G", "I"}}







                  share|improve this answer



























                    up vote
                    2
                    down vote













                    listz = CharacterRange["A", "K"];
                    k = 4;

                    SeedRandom[1]
                    rc = RandomChoice[Range @ k, Length @ listz];
                    {m, n, p, r} = Pick[listz, rc, #]& /@ Range[k]



                    {{"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}, {}, {"A", "J"}}




                    Also



                    Lookup[GroupBy[Transpose[{rc, listz}], First -> Last], #, {}]& /@ Range[k] 
                    AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                    both give the same result. Also, using Carl Woll's GatherByList:



                    RandomSample[PadRight[GatherByList[listz, rc], {k, Automatic}, {}]]



                    {{"A", "J"}, {}, {"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}}




                    Update: To get k non-empty sublists:



                    SeedRandom[1]
                    rc = Module[{r}, While[Length[Counts[r = RandomChoice[Range@k, Length @ listz]]] < k]; r];

                    Pick[listz , rc, # ] & /@ Range[k]
                    Lookup[GroupBy[Transpose[{rc , listz}], First -> Last], #, {}] & /@ Range[k]
                    AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                    all give




                    {{"A", "B", "C", "E", "H", "I"}, {"F"}, {"D", "G"}, {"J", "K"}}




                    GatherByList[listz, rc] 



                    {{"A", "B", "C", "E", "H", "I"}, {"D", "G"}, {"F"}, {"J", "K"}}




                    Update 2:



                    You can also use the function RandomKSetPartition from the Combinatorica package:



                    Quiet @ Needs["Combinatorica`"]
                    SeedRandom[1]
                    RandomKSetPartition[listz, 4]



                    {{"A", "D", "H"}, {"B"}, {"C", "F", "J", "K"}, {"E", "G", "I"}}







                    share|improve this answer

























                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      listz = CharacterRange["A", "K"];
                      k = 4;

                      SeedRandom[1]
                      rc = RandomChoice[Range @ k, Length @ listz];
                      {m, n, p, r} = Pick[listz, rc, #]& /@ Range[k]



                      {{"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}, {}, {"A", "J"}}




                      Also



                      Lookup[GroupBy[Transpose[{rc, listz}], First -> Last], #, {}]& /@ Range[k] 
                      AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                      both give the same result. Also, using Carl Woll's GatherByList:



                      RandomSample[PadRight[GatherByList[listz, rc], {k, Automatic}, {}]]



                      {{"A", "J"}, {}, {"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}}




                      Update: To get k non-empty sublists:



                      SeedRandom[1]
                      rc = Module[{r}, While[Length[Counts[r = RandomChoice[Range@k, Length @ listz]]] < k]; r];

                      Pick[listz , rc, # ] & /@ Range[k]
                      Lookup[GroupBy[Transpose[{rc , listz}], First -> Last], #, {}] & /@ Range[k]
                      AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                      all give




                      {{"A", "B", "C", "E", "H", "I"}, {"F"}, {"D", "G"}, {"J", "K"}}




                      GatherByList[listz, rc] 



                      {{"A", "B", "C", "E", "H", "I"}, {"D", "G"}, {"F"}, {"J", "K"}}




                      Update 2:



                      You can also use the function RandomKSetPartition from the Combinatorica package:



                      Quiet @ Needs["Combinatorica`"]
                      SeedRandom[1]
                      RandomKSetPartition[listz, 4]



                      {{"A", "D", "H"}, {"B"}, {"C", "F", "J", "K"}, {"E", "G", "I"}}







                      share|improve this answer














                      listz = CharacterRange["A", "K"];
                      k = 4;

                      SeedRandom[1]
                      rc = RandomChoice[Range @ k, Length @ listz];
                      {m, n, p, r} = Pick[listz, rc, #]& /@ Range[k]



                      {{"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}, {}, {"A", "J"}}




                      Also



                      Lookup[GroupBy[Transpose[{rc, listz}], First -> Last], #, {}]& /@ Range[k] 
                      AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                      both give the same result. Also, using Carl Woll's GatherByList:



                      RandomSample[PadRight[GatherByList[listz, rc], {k, Automatic}, {}]]



                      {{"A", "J"}, {}, {"C", "F", "G", "H", "K"}, {"B", "D", "E", "I"}}




                      Update: To get k non-empty sublists:



                      SeedRandom[1]
                      rc = Module[{r}, While[Length[Counts[r = RandomChoice[Range@k, Length @ listz]]] < k]; r];

                      Pick[listz , rc, # ] & /@ Range[k]
                      Lookup[GroupBy[Transpose[{rc , listz}], First -> Last], #, {}] & /@ Range[k]
                      AdjacencyList[Graph[Range[k], Thread[rc -> listz]], # ] & /@ Range[k]


                      all give




                      {{"A", "B", "C", "E", "H", "I"}, {"F"}, {"D", "G"}, {"J", "K"}}




                      GatherByList[listz, rc] 



                      {{"A", "B", "C", "E", "H", "I"}, {"D", "G"}, {"F"}, {"J", "K"}}




                      Update 2:



                      You can also use the function RandomKSetPartition from the Combinatorica package:



                      Quiet @ Needs["Combinatorica`"]
                      SeedRandom[1]
                      RandomKSetPartition[listz, 4]



                      {{"A", "D", "H"}, {"B"}, {"C", "F", "J", "K"}, {"E", "G", "I"}}








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 days ago

























                      answered Nov 4 at 21:47









                      kglr

                      170k8193396




                      170k8193396






















                          up vote
                          1
                          down vote













                          Edit: I realized that my previous solution doesn't produce the same length partitions. Here is the desired solution.



                          s = RandomSample[Range@10, 10];
                          t = RandomSample@RandomChoice@IntegerPartitions[10, {4}]
                          {m, n, p, r} = TakeList[s, t]

                          (*or {m, n, p, r} = FoldPairList[TakeDrop, s, t]*)



                          {{1, 5, 10}, {3, 7}, {2, 9, 6, 8}, {4}}




                          Original answer:



                          s = RandomSample[Range@10, 10];
                          t = RandomSample[Range@4, 4];
                          {m, n, p, r} = FoldPairList[TakeDrop, s, t]



                          {{6, 2}, {9}, {5, 1, 7}, {10, 3, 8, 4}}







                          share|improve this answer



























                            up vote
                            1
                            down vote













                            Edit: I realized that my previous solution doesn't produce the same length partitions. Here is the desired solution.



                            s = RandomSample[Range@10, 10];
                            t = RandomSample@RandomChoice@IntegerPartitions[10, {4}]
                            {m, n, p, r} = TakeList[s, t]

                            (*or {m, n, p, r} = FoldPairList[TakeDrop, s, t]*)



                            {{1, 5, 10}, {3, 7}, {2, 9, 6, 8}, {4}}




                            Original answer:



                            s = RandomSample[Range@10, 10];
                            t = RandomSample[Range@4, 4];
                            {m, n, p, r} = FoldPairList[TakeDrop, s, t]



                            {{6, 2}, {9}, {5, 1, 7}, {10, 3, 8, 4}}







                            share|improve this answer

























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Edit: I realized that my previous solution doesn't produce the same length partitions. Here is the desired solution.



                              s = RandomSample[Range@10, 10];
                              t = RandomSample@RandomChoice@IntegerPartitions[10, {4}]
                              {m, n, p, r} = TakeList[s, t]

                              (*or {m, n, p, r} = FoldPairList[TakeDrop, s, t]*)



                              {{1, 5, 10}, {3, 7}, {2, 9, 6, 8}, {4}}




                              Original answer:



                              s = RandomSample[Range@10, 10];
                              t = RandomSample[Range@4, 4];
                              {m, n, p, r} = FoldPairList[TakeDrop, s, t]



                              {{6, 2}, {9}, {5, 1, 7}, {10, 3, 8, 4}}







                              share|improve this answer














                              Edit: I realized that my previous solution doesn't produce the same length partitions. Here is the desired solution.



                              s = RandomSample[Range@10, 10];
                              t = RandomSample@RandomChoice@IntegerPartitions[10, {4}]
                              {m, n, p, r} = TakeList[s, t]

                              (*or {m, n, p, r} = FoldPairList[TakeDrop, s, t]*)



                              {{1, 5, 10}, {3, 7}, {2, 9, 6, 8}, {4}}




                              Original answer:



                              s = RandomSample[Range@10, 10];
                              t = RandomSample[Range@4, 4];
                              {m, n, p, r} = FoldPairList[TakeDrop, s, t]



                              {{6, 2}, {9}, {5, 1, 7}, {10, 3, 8, 4}}








                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 4 at 21:37

























                              answered Nov 4 at 20:16









                              Okkes Dulgerci

                              3,4061716




                              3,4061716






















                                  up vote
                                  0
                                  down vote













                                  A fast and short way is to utilize Pick. For this, we will need two commands:



                                  ClearAll[distribute];
                                  distribute[list_, partNum_] := RandomInteger[partNum - 1, Length@list]


                                  creates a number sequence which will assign each element to a list. And we do this with



                                  ClearAll[splitList];
                                  splitList[list_, partitionNumber_] := With[
                                  {selection = distribute[list, partitionNumber]},
                                  Table[Pick[list, selection, i], {i, 0, partitionNumber - 1}]
                                  ]


                                  That's it. The code is fast enough; on my laptop, it takes less than half a second to split a list of $10^7$ elements into 3 lists:



                                  splitList[Range[10^7], 3] // RepeatedTiming
                                  (* {0.42,...} *)


                                  It becomes slower if the number of list increases, as it is not optimized in that direction. Still, it should be fast enough, for example:



                                  splitList[Range[10^7], 10^3] // RepeatedTiming
                                  (* {8.3,...} *)





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    A fast and short way is to utilize Pick. For this, we will need two commands:



                                    ClearAll[distribute];
                                    distribute[list_, partNum_] := RandomInteger[partNum - 1, Length@list]


                                    creates a number sequence which will assign each element to a list. And we do this with



                                    ClearAll[splitList];
                                    splitList[list_, partitionNumber_] := With[
                                    {selection = distribute[list, partitionNumber]},
                                    Table[Pick[list, selection, i], {i, 0, partitionNumber - 1}]
                                    ]


                                    That's it. The code is fast enough; on my laptop, it takes less than half a second to split a list of $10^7$ elements into 3 lists:



                                    splitList[Range[10^7], 3] // RepeatedTiming
                                    (* {0.42,...} *)


                                    It becomes slower if the number of list increases, as it is not optimized in that direction. Still, it should be fast enough, for example:



                                    splitList[Range[10^7], 10^3] // RepeatedTiming
                                    (* {8.3,...} *)





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      A fast and short way is to utilize Pick. For this, we will need two commands:



                                      ClearAll[distribute];
                                      distribute[list_, partNum_] := RandomInteger[partNum - 1, Length@list]


                                      creates a number sequence which will assign each element to a list. And we do this with



                                      ClearAll[splitList];
                                      splitList[list_, partitionNumber_] := With[
                                      {selection = distribute[list, partitionNumber]},
                                      Table[Pick[list, selection, i], {i, 0, partitionNumber - 1}]
                                      ]


                                      That's it. The code is fast enough; on my laptop, it takes less than half a second to split a list of $10^7$ elements into 3 lists:



                                      splitList[Range[10^7], 3] // RepeatedTiming
                                      (* {0.42,...} *)


                                      It becomes slower if the number of list increases, as it is not optimized in that direction. Still, it should be fast enough, for example:



                                      splitList[Range[10^7], 10^3] // RepeatedTiming
                                      (* {8.3,...} *)





                                      share|improve this answer












                                      A fast and short way is to utilize Pick. For this, we will need two commands:



                                      ClearAll[distribute];
                                      distribute[list_, partNum_] := RandomInteger[partNum - 1, Length@list]


                                      creates a number sequence which will assign each element to a list. And we do this with



                                      ClearAll[splitList];
                                      splitList[list_, partitionNumber_] := With[
                                      {selection = distribute[list, partitionNumber]},
                                      Table[Pick[list, selection, i], {i, 0, partitionNumber - 1}]
                                      ]


                                      That's it. The code is fast enough; on my laptop, it takes less than half a second to split a list of $10^7$ elements into 3 lists:



                                      splitList[Range[10^7], 3] // RepeatedTiming
                                      (* {0.42,...} *)


                                      It becomes slower if the number of list increases, as it is not optimized in that direction. Still, it should be fast enough, for example:



                                      splitList[Range[10^7], 10^3] // RepeatedTiming
                                      (* {8.3,...} *)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 days ago









                                      Soner

                                      77349




                                      77349






















                                          ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.










                                           

                                          draft saved


                                          draft discarded


















                                          ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.













                                          ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.












                                          ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.















                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185296%2frandomly-distribute-numbers-to-lists%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest




















































































                                          這個網誌中的熱門文章

                                          Tangent Lines Diagram Along Smooth Curve

                                          Yusuf al-Mu'taman ibn Hud

                                          Zucchini