From: "John D'Errico" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Generating Random numbers
Date: Thu, 2 Jul 2009 12:45:02 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 43
Message-ID: <h2ia4e$3at$>
References: <> <>
Reply-To: "John D'Errico" <>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1246538702 3421 (2 Jul 2009 12:45:02 GMT)
NNTP-Posting-Date: Thu, 2 Jul 2009 12:45:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: comp.soft-sys.matlab:552357

Deivy <> wrote in message <>...
> Hi Sadik,
> Thanks for the reply.
> The choice of 3000 was accidental and it made the case bit easier.
> What if the total is 4000, where it is not directly related to any of
> the numbers 500 and 600, and how do I do that.

You should recognize that this is not possible in some
circumstances, and certainly will not be a random set
of numbers. 

For example, write the number 2401 as the sum of ANY
size set of numbers in the range [500,600]. It cannot
be done.

Or, write 2500 as such a sum? It turns out there is only
one such solution, 2500 = 500 + 500 + 500 + 500 + 500.
You may not think of this solution as a random set.

Regardless, even if a solution exists that is not unique,
it is not random in the common sense of the word, since
given the first n-1 members of the set, the last member
is uniquely determined to meet the sum criterion.

If you still insist on finding a random looking set of
numbers that satisfy your sum constraint, use a simple

How many terms will the sum be composed of?

7*500 = 3500, and 8*500 = 4000.
7*600 = 4200

Therefore, the sum must be composed of 7 numbers in
that range.

 x = 500 + 100*rand(1,7);
 x = x * 4000/sum(x);