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}
list-manipulation random education
New contributor
add a comment |
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}
list-manipulation random education
New contributor
Why not just generate four random lists in the first place?
– gothicVI
Nov 4 at 20:00
add a comment |
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}
list-manipulation random education
New contributor
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
list-manipulation random education
New contributor
New contributor
New contributor
asked Nov 4 at 19:58
ithilquessirr
473
473
New contributor
New contributor
Why not just generate four random lists in the first place?
– gothicVI
Nov 4 at 20:00
add a comment |
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
add a comment |
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
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 BecauseSelect[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
add a comment |
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]]
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 inspectMatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]]
to find that out.
– Henrik Schumacher
Nov 4 at 20:52
add a comment |
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}}
add a comment |
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"}}
add a comment |
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}}
add a comment |
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,...} *)
add a comment |
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
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 BecauseSelect[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
add a comment |
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
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 BecauseSelect[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
add a comment |
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
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
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 BecauseSelect[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
add a comment |
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 BecauseSelect[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
add a comment |
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]]
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 inspectMatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]]
to find that out.
– Henrik Schumacher
Nov 4 at 20:52
add a comment |
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]]
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 inspectMatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]]
to find that out.
– Henrik Schumacher
Nov 4 at 20:52
add a comment |
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]]
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]]
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 inspectMatrixForm@Normal[SparseArray[Transpose[{s, listz}] -> _, {mm, nn}]]
to find that out.
– Henrik Schumacher
Nov 4 at 20:52
add a comment |
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 inspectMatrixForm@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
add a comment |
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}}
add a comment |
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}}
add a comment |
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}}
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}}
answered Nov 5 at 16:04
Carl Woll
64.3k283167
64.3k283167
add a comment |
add a comment |
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"}}
add a comment |
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"}}
add a comment |
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"}}
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"}}
edited 2 days ago
answered Nov 4 at 21:47
kglr
170k8193396
170k8193396
add a comment |
add a comment |
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}}
add a comment |
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}}
add a comment |
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}}
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}}
edited Nov 4 at 21:37
answered Nov 4 at 20:16
Okkes Dulgerci
3,4061716
3,4061716
add a comment |
add a comment |
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,...} *)
add a comment |
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,...} *)
add a comment |
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,...} *)
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,...} *)
answered 2 days ago
Soner
77349
77349
add a comment |
add a comment |
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.
ithilquessirr is a new contributor. Be nice, and check out our Code of Conduct.
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
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
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
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
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
Why not just generate four random lists in the first place?
– gothicVI
Nov 4 at 20:00