Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generating Random numbers

Subject: Generating Random numbers

From: Deivy

Date: 2 Jul, 2009 11:24:11

Message: 1 of 11

Hi Group,
I would like to generate a set of numbers between 500 and 600 so that
the total of them would be 3000.
How many numbers to generate is unknown but the generated numbers
should be within the range of 500 to 600 they should total to 3000.
How do I do this in matlab.
Could someone help me with this.

A small script that accomplishes this task is highly appreciated.

Thank you,
Regards,
Deivy

Subject: Generating Random numbers

From: arun

Date: 2 Jul, 2009 11:53:08

Message: 2 of 11

On Jul 2, 1:24 pm, Deivy <deiv...@gmail.com> wrote:
> Hi Group,
> I would like to generate a set of numbers between 500 and 600 so that
> the total of them would be 3000.
> How many numbers to generate is unknown but the generated numbers
> should be within the range of 500 to 600 they should total to 3000.
> How do I do this in matlab.
> Could someone help me with this.
>
> A small script that accomplishes this task is highly appreciated.
>
> Thank you,
> Regards,
> Deivy

3000 = 500 * 6 = 600 * 5.

so, the numbers you generate should be able to accomplish the task of
getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
would exceed 3000. so, its either 5 or 6. and they are not random, I
suppose.. unless the sum is >= 3000 but not == 3000.

best,
arun.

Subject: Generating Random numbers

From: Deivy

Date: 2 Jul, 2009 11:58:01

Message: 3 of 11

Hi Arun,
Thanks for the reply.

Thanks Deivy


On Jul 2, 1:53 pm, arun <aragorn1...@gmail.com> wrote:
> On Jul 2, 1:24 pm, Deivy <deiv...@gmail.com> wrote:
>
> > Hi Group,
> > I would like to generate a set of numbers between 500 and 600 so that
> > the total of them would be 3000.
> > How many numbers to generate is unknown but the generated numbers
> > should be within the range of 500 to 600 they should total to 3000.
> > How do I do this in matlab.
> > Could someone help me with this.
>
> > A small script that accomplishes this task is highly appreciated.
>
> > Thank you,
> > Regards,
> > Deivy
>
> 3000 = 500 * 6 = 600 * 5.
>
> so, the numbers you generate should be able to accomplish the task of
> getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
> would exceed 3000. so, its either 5 or 6. and they are not random, I
> suppose.. unless the sum is >= 3000 but not == 3000.
>
> best,
> arun.

Subject: Generating Random numbers

From: Sadik

Date: 2 Jul, 2009 12:06:00

Message: 4 of 11

Hi Deivy,

You actually have two sets only.

A = {500,500,500,500,500,500}

and

B = {600,600,600,600,600}

The best way to see it is this.

Average is the sum divided by the number of elements, right?

Therefore, in any case, you can have at most 6 elements and at least 5 elements. This means that you have to have corresponding averages of 500 and 600.

Looking at set A, the only way that you can achieve the average value of 500 is that all are equal to 500. Otherwise, you will have elements going below 500, which is not acceptable.

A similar argument is valid for set B.

Hope this helps.

Deivy <deivy71@gmail.com> wrote in message <4d2b33d7-9115-449a-bd83-5ec410153f0b@h8g2000yqm.googlegroups.com>...
> Hi Arun,
> Thanks for the reply.
>
> Thanks Deivy
>
>
> On Jul 2, 1:53?pm, arun <aragorn1...@gmail.com> wrote:
> > On Jul 2, 1:24?pm, Deivy <deiv...@gmail.com> wrote:
> >
> > > Hi Group,
> > > I would like to generate a set of numbers between 500 and 600 so that
> > > the total of them would be 3000.
> > > How many numbers to generate is unknown but the generated numbers
> > > should be within the range of 500 to 600 they should total to 3000.
> > > How do I do this in matlab.
> > > Could someone help me with this.
> >
> > > A small script that accomplishes this task is highly appreciated.
> >
> > > Thank you,
> > > Regards,
> > > Deivy
> >
> > 3000 = 500 * 6 = 600 * 5.
> >
> > so, the numbers you generate should be able to accomplish the task of
> > getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
> > would exceed 3000. so, its either 5 or 6. and they are not random, I
> > suppose.. unless the sum is >= 3000 but not == 3000.
> >
> > best,
> > arun.

Subject: Generating Random numbers

From: Deivy

Date: 2 Jul, 2009 12:13:30

Message: 5 of 11


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.

Thank you,
Regards,
Deivy


On Jul 2, 2:06 pm, "Sadik " <sadik.h...@gmail.com> wrote:
> Hi Deivy,
>
> You actually have two sets only.
>
> A = {500,500,500,500,500,500}
>
> and
>
> B = {600,600,600,600,600}
>
> The best way to see it is this.
>
> Average is the sum divided by the number of elements, right?
>
> Therefore, in any case, you can have at most 6 elements and at least 5 elements. This means that you have to have corresponding averages of 500 and 600.
>
> Looking at set A, the only way that you can achieve the average value of 500 is that all are equal to 500. Otherwise, you will have elements going below 500, which is not acceptable.
>
> A similar argument is valid for set B.
>
> Hope this helps.
>
>
>
> Deivy <deiv...@gmail.com> wrote in message <4d2b33d7-9115-449a-bd83-5ec410153...@h8g2000yqm.googlegroups.com>...
> > Hi Arun,
> > Thanks for the reply.
>
> > Thanks Deivy
>
> > On Jul 2, 1:53?pm, arun <aragorn1...@gmail.com> wrote:
> > > On Jul 2, 1:24?pm, Deivy <deiv...@gmail.com> wrote:
>
> > > > Hi Group,
> > > > I would like to generate a set of numbers between 500 and 600 so that
> > > > the total of them would be 3000.
> > > > How many numbers to generate is unknown but the generated numbers
> > > > should be within the range of 500 to 600 they should total to 3000.
> > > > How do I do this in matlab.
> > > > Could someone help me with this.
>
> > > > A small script that accomplishes this task is highly appreciated.
>
> > > > Thank you,
> > > > Regards,
> > > > Deivy
>
> > > 3000 = 500 * 6 = 600 * 5.
>
> > > so, the numbers you generate should be able to accomplish the task of
> > > getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
> > > would exceed 3000. so, its either 5 or 6. and they are not random, I
> > > suppose.. unless the sum is >= 3000 but not == 3000.
>
> > > best,
> > > arun.

Subject: Generating Random numbers

From: Sadik

Date: 2 Jul, 2009 12:41:01

Message: 6 of 11

One thing you could do is the following:

A = (ceil(499.5+100*rand(10000,7)));
indicesOf4000 = find(sum(A,2)==4000);

Then, you could see the set of numbers by:

for k = 1:length(indicesOf4000)
    disp(num2str(A(indicesOf4000,:)))
end

If indicesOf4000 seems to be blank, you could either run A = ... again, or you could increase the number of sample sets from 10000 to a higher value.

Best.


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.
>
> Thank you,
> Regards,
> Deivy
>
>
> On Jul 2, 2:06?pm, "Sadik " <sadik.h...@gmail.com> wrote:
> > Hi Deivy,
> >
> > You actually have two sets only.
> >
> > A = {500,500,500,500,500,500}
> >
> > and
> >
> > B = {600,600,600,600,600}
> >
> > The best way to see it is this.
> >
> > Average is the sum divided by the number of elements, right?
> >
> > Therefore, in any case, you can have at most 6 elements and at least 5 elements. This means that you have to have corresponding averages of 500 and 600.
> >
> > Looking at set A, the only way that you can achieve the average value of 500 is that all are equal to 500. Otherwise, you will have elements going below 500, which is not acceptable.
> >
> > A similar argument is valid for set B.
> >
> > Hope this helps.
> >
> >
> >
> > Deivy <deiv...@gmail.com> wrote in message <4d2b33d7-9115-449a-bd83-5ec410153...@h8g2000yqm.googlegroups.com>...
> > > Hi Arun,
> > > Thanks for the reply.
> >
> > > Thanks Deivy
> >
> > > On Jul 2, 1:53?pm, arun <aragorn1...@gmail.com> wrote:
> > > > On Jul 2, 1:24?pm, Deivy <deiv...@gmail.com> wrote:
> >
> > > > > Hi Group,
> > > > > I would like to generate a set of numbers between 500 and 600 so that
> > > > > the total of them would be 3000.
> > > > > How many numbers to generate is unknown but the generated numbers
> > > > > should be within the range of 500 to 600 they should total to 3000.
> > > > > How do I do this in matlab.
> > > > > Could someone help me with this.
> >
> > > > > A small script that accomplishes this task is highly appreciated.
> >
> > > > > Thank you,
> > > > > Regards,
> > > > > Deivy
> >
> > > > 3000 = 500 * 6 = 600 * 5.
> >
> > > > so, the numbers you generate should be able to accomplish the task of
> > > > getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
> > > > would exceed 3000. so, its either 5 or 6. and they are not random, I
> > > > suppose.. unless the sum is >= 3000 but not == 3000.
> >
> > > > best,
> > > > arun.

Subject: Generating Random numbers

From: Sadik

Date: 2 Jul, 2009 12:45:02

Message: 7 of 11

The display line should have been:

disp(num2str(A(indicesOf4000(k),:)))

Hope this helps.


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.
>
> Thank you,
> Regards,
> Deivy
>
>
> On Jul 2, 2:06?pm, "Sadik " <sadik.h...@gmail.com> wrote:
> > Hi Deivy,
> >
> > You actually have two sets only.
> >
> > A = {500,500,500,500,500,500}
> >
> > and
> >
> > B = {600,600,600,600,600}
> >
> > The best way to see it is this.
> >
> > Average is the sum divided by the number of elements, right?
> >
> > Therefore, in any case, you can have at most 6 elements and at least 5 elements. This means that you have to have corresponding averages of 500 and 600.
> >
> > Looking at set A, the only way that you can achieve the average value of 500 is that all are equal to 500. Otherwise, you will have elements going below 500, which is not acceptable.
> >
> > A similar argument is valid for set B.
> >
> > Hope this helps.
> >
> >
> >
> > Deivy <deiv...@gmail.com> wrote in message <4d2b33d7-9115-449a-bd83-5ec410153...@h8g2000yqm.googlegroups.com>...
> > > Hi Arun,
> > > Thanks for the reply.
> >
> > > Thanks Deivy
> >
> > > On Jul 2, 1:53?pm, arun <aragorn1...@gmail.com> wrote:
> > > > On Jul 2, 1:24?pm, Deivy <deiv...@gmail.com> wrote:
> >
> > > > > Hi Group,
> > > > > I would like to generate a set of numbers between 500 and 600 so that
> > > > > the total of them would be 3000.
> > > > > How many numbers to generate is unknown but the generated numbers
> > > > > should be within the range of 500 to 600 they should total to 3000.
> > > > > How do I do this in matlab.
> > > > > Could someone help me with this.
> >
> > > > > A small script that accomplishes this task is highly appreciated.
> >
> > > > > Thank you,
> > > > > Regards,
> > > > > Deivy
> >
> > > > 3000 = 500 * 6 = 600 * 5.
> >
> > > > so, the numbers you generate should be able to accomplish the task of
> > > > getting a sum of 3000 in 5 or 6 random numbers, otherwise the sum
> > > > would exceed 3000. so, its either 5 or 6. and they are not random, I
> > > > suppose.. unless the sum is >= 3000 but not == 3000.
> >
> > > > best,
> > > > arun.

Subject: Generating Random numbers

From: John D'Errico

Date: 2 Jul, 2009 12:45:02

Message: 8 of 11

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

Subject: Generating Random numbers

From: Deivy

Date: 2 Jul, 2009 13:18:49

Message: 9 of 11

Hi Sadik & John,
Thank you for your replies.

Sadik,
when such a solution exists how do I extract to another matrix or wite
to a text file.

Thank you,
Regards,
Deivy





On Jul 2, 2:45 pm, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> Deivy <deiv...@gmail.com> wrote in message <1602c78c-e855-4e3b-a483-4fa3ecba6...@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

Subject: Generating Random numbers

From: Sadik

Date: 2 Jul, 2009 13:34:01

Message: 10 of 11

B = A(indicesOf4000,:);

should give you the matrix.

In order to write to the text file, you need to do the following:

fid = fopen('C:\4000.txt','w');
fprintf(fid,'%d %d %d %d %d %d %d \n',A(indicesOf4000,:));
fclose(fid);

You could open the file using wordpad and you will hopefully see the numbers in a regular manner.

Hope this helps.


Deivy <deivy71@gmail.com> wrote in message <8f91f9c6-aacc-4e38-92d7-1608eeb61076@a7g2000yqk.googlegroups.com>...
> Hi Sadik & John,
> Thank you for your replies.
>
> Sadik,
> when such a solution exists how do I extract to another matrix or wite
> to a text file.
>
> Thank you,
> Regards,
> Deivy
>
>
>
>
>
> On Jul 2, 2:45?pm, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> > Deivy <deiv...@gmail.com> wrote in message <1602c78c-e855-4e3b-a483-4fa3ecba6...@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

Subject: Generating Random numbers

From: Deivy

Date: 2 Jul, 2009 13:37:32

Message: 11 of 11

Hi Sadik,
Thank you very much.

I am still tring and the code works fine but for different numbers and
ranges I have to tweak I guess.
Anyway, Thanks a lot for all the help.


Thank you,
Regards,
Deivy



On Jul 2, 3:34 pm, "Sadik " <sadik.h...@gmail.com> wrote:
> B = A(indicesOf4000,:);
>
> should give you the matrix.
>
> In order to write to the text file, you need to do the following:
>
> fid = fopen('C:\4000.txt','w');
> fprintf(fid,'%d %d %d %d %d %d %d \n',A(indicesOf4000,:));
> fclose(fid);
>
> You could open the file using wordpad and you will hopefully see the numbers in a regular manner.
>
> Hope this helps.
>
>
>
> Deivy <deiv...@gmail.com> wrote in message <8f91f9c6-aacc-4e38-92d7-1608eeb61...@a7g2000yqk.googlegroups.com>...
> > Hi Sadik & John,
> > Thank you for your replies.
>
> > Sadik,
> > when such a solution exists how do I extract to another matrix or wite
> > to a text file.
>
> > Thank you,
> > Regards,
> > Deivy
>
> > On Jul 2, 2:45?pm, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> > > Deivy <deiv...@gmail.com> wrote in message <1602c78c-e855-4e3b-a483-4fa3ecba6...@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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us