First, I generate random numbers (let say 1000 elements), how to know how many first elements that have sum is smaller or greater than 500?
Second, I will generate 4 lists of random numbers. Every number distributed in each list has no repetition, and if sum of the all lists are arise fixed value, the random generating will be stoped. example: 1-4 lists; (1,2,3,4,5), (6,7,8,9,10), (11,12,13,14,15), (16,17,18,19,20); sum of 4 lists is 210.
How I run the comand like "Give me 4 lists random numbers, no repetiton, if the sum = 210 stop random."?
thank you for the answer
This reports the number of first elements of rand
to exceed 500.
rand = RandomInteger[10, 1000];
Catch[total = 0;
Map[If[(total += First[#]) > 500, Throw[#[[2, 1]]]] &,
MapIndexed[List, rand]]]
103
Second question
"Every number distributed in each lists has no repetition."
RandomSample
is useful for this. Adding a counter n
, can be omitted.
n = 0;
While[True,
n++;
total = Total@Flatten[lists = Table[RandomSample[Range[50], 5], 4]];
If[total < 500, Break[]]]
lists
Row[{n, " loops"}]
{{36, 30, 25, 9, 10}, {36, 22, 44, 13, 2}, {9, 24, 34, 7, 47}, {5, 28, 15, 36, 22}}
4 loops
36 appears in 2 lists, but each list itself has no repetitions.