Randomly distribute numbers to lists
Clash Royale CLAN TAG#URR8PPP
up vote
2
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
2
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
2 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
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 2 hours ago
ithilquessirr
282
282
New contributor
New contributor
Why not just generate four random lists in the first place?
â gothicVI
2 hours ago
add a comment |Â
Why not just generate four random lists in the first place?
â gothicVI
2 hours ago
Why not just generate four random lists in the first place?
â gothicVI
2 hours ago
Why not just generate four random lists in the first place?
â gothicVI
2 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
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
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 BecauseSelect[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.
â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
add a comment |Â
up vote
1
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
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspectMatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.
â Henrik Schumacher
1 hour ago
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
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]
"C", "F", "G", "H", "K", "B", "D", "E", "I", , "A", "J"
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 ]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
Lookup[GroupBy[Transpose[rc , listz], First -> Last], #, ] & /@ Range[k]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 BecauseSelect[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.
â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
add a comment |Â
up vote
2
down vote
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
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 BecauseSelect[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.
â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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 54 mins ago
answered 1 hour ago
J42161217
2,447218
2,447218
Have you tried that with a list of 100 elements?
â Henrik Schumacher
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 BecauseSelect[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.
â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
add a comment |Â
Have you tried that with a list of 100 elements?
â Henrik Schumacher
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 BecauseSelect[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.
â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
Have you tried that with a list of 100 elements?
â Henrik Schumacher
1 hour ago
Have you tried that with a list of 100 elements?
â Henrik Schumacher
1 hour ago
how about 10^100?
â J42161217
1 hour ago
how about 10^100?
â J42161217
1 hour ago
@J42161217 Because
Select[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.â Okkes Dulgerci
1 hour ago
@J42161217 Because
Select[IntegerPartitions@10, Length@# == 4 &]
sorted, you cannot get Lenght=1or 2 set for m.â Okkes Dulgerci
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
@OkkesDulgerci fixed!
â J42161217
1 hour ago
add a comment |Â
up vote
1
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
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspectMatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.
â Henrik Schumacher
1 hour ago
add a comment |Â
up vote
1
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
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspectMatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.
â Henrik Schumacher
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
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 1 hour ago
answered 2 hours ago
Henrik Schumacher
43.9k263129
43.9k263129
Is this supposed to spew errors in 11.3?
â user6014
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspectMatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.
â Henrik Schumacher
1 hour ago
add a comment |Â
Is this supposed to spew errors in 11.3?
â user6014
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspectMatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.
â Henrik Schumacher
1 hour ago
Is this supposed to spew errors in 11.3?
â user6014
2 hours ago
Is this supposed to spew errors in 11.3?
â user6014
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Ah, sorry. Please try again.
â Henrik Schumacher
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Sorry, I couldn't understand what sparsearray function does here. But thanks this code is working.
â ithilquessirr
2 hours ago
Well, you might inspect
MatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.â Henrik Schumacher
1 hour ago
Well, you might inspect
MatrixForm@Normal[SparseArray[Transpose[s, listz] -> _, mm, nn]]
to find that out.â Henrik Schumacher
1 hour ago
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 1 hour ago
answered 2 hours ago
Okkes Dulgerci
3,4061716
3,4061716
add a comment |Â
add a comment |Â
up vote
1
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]
"C", "F", "G", "H", "K", "B", "D", "E", "I", , "A", "J"
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 ]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
Lookup[GroupBy[Transpose[rc , listz], First -> Last], #, ] & /@ Range[k]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
add a comment |Â
up vote
1
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]
"C", "F", "G", "H", "K", "B", "D", "E", "I", , "A", "J"
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 ]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
Lookup[GroupBy[Transpose[rc , listz], First -> Last], #, ] & /@ Range[k]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
add a comment |Â
up vote
1
down vote
up vote
1
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]
"C", "F", "G", "H", "K", "B", "D", "E", "I", , "A", "J"
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 ]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
Lookup[GroupBy[Transpose[rc , listz], First -> Last], #, ] & /@ Range[k]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
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]
"C", "F", "G", "H", "K", "B", "D", "E", "I", , "A", "J"
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 ]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
Lookup[GroupBy[Transpose[rc , listz], First -> Last], #, ] & /@ Range[k]
"A", "B", "C", "E", "H", "I", "F", "D", "G", "J", "K"
edited 3 mins ago
answered 52 mins ago
kglr
169k8192396
169k8192396
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
2 hours ago