Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
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$1@fred.mathworks.com>
References: <d639c2c7-c672-4177-b89b-5c584755dabc@x5g2000yqk.googlegroups.com> <1602c78c-e855-4e3b-a483-4fa3ecba6ddb@h8g2000yqm.googlegroups.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246538702 3421 172.30.248.38 (2 Jul 2009 12:45:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 2 Jul 2009 12:45:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:552357

Deivy <deivy71@gmail.com> wrote in message <1602c78c-e855-4e3b-a483-4fa3ecba6ddb@h8g2000yqm.googlegroups.com>...
> 
> 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
logic:

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);

John