1

Possible Duplicate:
Random numbers that add to 100: Matlab

I am looking to do the following:

Generate several random numbers between 10 and 50.

Have the sum of the random numbers equal 500.

What I have so far is:

sum = 500
n=5
b = [ ]
for i = 1:1:n-1,
a = randi([10,50])
b = [b a]
end

n=5 is in there as an example and can equal anything.

I am looking for the sum of b to equal 500 (That could be the difference between 'sum = 500' and sum of the values before the sum equals 500 -- making the last number not actually random, which is ok)

b =

23    10    43    30
Community
  • 1
  • 1
Kenny
  • 43
  • 5

1 Answers1

1

Check out this nice random vectors generator with fixed sum FEX file. I think this will answer your question.

bla
  • 25,846
  • 10
  • 70
  • 101
  • This looks great. Just need to figure out how to use it. Thanks! – Kenny Nov 04 '12 at 02:49
  • are the five variables contained in the following: randfixedsum(n,m,s,a,b) -- all that needs to be changed? – Kenny Nov 04 '12 at 03:00
  • see http://stackoverflow.com/questions/8064629/random-numbers-that-add-to-100-matlab for a more detailed answer... – bla Nov 04 '12 at 03:33