3
$\begingroup$

I am trying to get a list of numbers with at least 1000 sums as possible, using random numbers from 1 to 10. But I don't want to get blank list output.

How can I only get a list of numbers greater than 1000 in total? I think my loop is insufficient.

Here is my code:

m = {};
n = RandomInteger[{1, 10}, 170];
liste = Total[n];
While[liste >= 1000, AppendTo[m, n]; Break[];]
m
liste
$\endgroup$
  • 3
    $\begingroup$ Yes, I think your loop is insufficient, too :) It's not perfectly clear to me what you want, but maybe it's this?: While[n = RandomInteger[{1, 10}, 170]; Total[n] < 1000,]; n $\endgroup$ – Michael E2 Nov 4 '18 at 14:01
  • $\begingroup$ RandomInteger[{1, 10}, 1000] is guaranteed to work. $\endgroup$ – AccidentalFourierTransform Nov 4 '18 at 14:54
  • $\begingroup$ Or: Reap[sum = 0; While[sum < 1000, sum += (n = RandomInteger[{1, 10}]); Sow[n]]][[2, 1]] $\endgroup$ – Daniel Lichtblau Nov 4 '18 at 15:04
3
$\begingroup$

if you want to output just one number with your conditions try

liste = 0;
While[liste < 1000, n = RandomInteger[{1, 10}, 170];
liste = Total[n]]
liste
n   

here is the result and the 170 numbers that add up to this result
If you don't want all these numbers to be displayed, just remove the last n from the code

1020

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

$\endgroup$
4
$\begingroup$
var = Array[x, 170];

dud = DiscreteUniformDistribution[{1, 10}];

Since you want the sum (Total) of the 170 variables to be at least 1000 and the most the sum could be is 1700, then the distribution for the truncated sum is

dist = TruncatedDistribution[{1000, 1700},
   TransformedDistribution[Total[var],
    Thread[Distributed[var, dud]]]];

To get a list of 1000 random draws from this distribution

SeedRandom[0]

list = RandomVariate[dist, 1000];

Mean[list] // N

(* 1015.98 *)

Median[list]

(* 1012 *)

Histogram[list]

enter image description here

$\endgroup$
1
$\begingroup$

One way of looking at your problem could be in terms of making a series of experiment. Then understanding the likelihood that the sum of 170 integers is greater than 1000. The code would then be:

res = Table[{i, RandomVariate[DiscreteUniformDistribution[{1, 10}], 170]}, {i,
 1, 100}]; 
Select[(Total@#[[2]]) >= 1000 &]@ res
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.