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:
Random Number Generation

Subject: Random Number Generation

From: Corey Adrian

Date: 12 Jul, 2010 16:59:04

Message: 1 of 8

I am trying to create a random number array given a minimum, maximum, and mean value. Is there any way to do this?

Thanks,
Corey

Subject: Random Number Generation

From: Steven Lord

Date: 12 Jul, 2010 17:11:01

Message: 2 of 8


"Corey Adrian" <uryguitarist777@yahoo.com> wrote in message
news:i1fhko$rsc$1@fred.mathworks.com...
>I am trying to create a random number array given a minimum, maximum, and
>mean value. Is there any way to do this?

Sure, just write a function that errors if the specified mean value is less
than the specified minimum or greater than the specified maximum value and
otherwise returns your mean value.

http://dilbert.com/strips/comic/2001-10-25/

If you want your numbers distributed differently in the interval between
your minimum and maximum than "centered at the mean value", you need to
provide a description of your desired distribution.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Random Number Generation

From: Roger Stafford

Date: 12 Jul, 2010 21:36:04

Message: 3 of 8

"Corey Adrian" <uryguitarist777@yahoo.com> wrote in message <i1fhko$rsc$1@fred.mathworks.com>...
> I am trying to create a random number array given a minimum, maximum, and mean value. Is there any way to do this?
>
> Thanks,
> Corey
- - - - - - - -
  How about using the beta distribution? Let a = minimum, b = maximum, and c = mean. Of course, it is assumed that a < c < b here. Then do:

 x = a+(b-a)*betarnd((c-a)/(b-a),(b-c)*(b-a),m,n);

to get an m by n array x of such random numbers.

  You can multiply the first two arguments of betarnd by any common factor to change the distribution's variance at your pleasure without affecting the mean. By making this factor very large you can approach Steven's interesting suggestion.

Roger Stafford

Subject: Random Number Generation

From: Roger Stafford

Date: 12 Jul, 2010 21:40:16

Message: 4 of 8

  Oops! I meant

 x = a+(b-a)*betarnd((c-a)/(b-a),(b-c)/(b-a),m,n);

Roger Stafford

Subject: Random Number Generation

From: Corey Adrian

Date: 12 Jul, 2010 22:30:11

Message: 5 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1g240$rho$1@fred.mathworks.com>...
> Oops! I meant
>
> x = a+(b-a)*betarnd((c-a)/(b-a),(b-c)/(b-a),m,n);
>
> Roger Stafford

Thanks Roger that is exactly what I needed!

Corey Adrian

Subject: Random Number Generation

From: taffi

Date: 26 Jun, 2013 16:06:06

Message: 6 of 8

Hey,

i need to use a std deviation as well. i have a max, min, mean and also a std value. How can i get random numbers of beta distribution wid a certain standard deviation.


"Corey Adrian" wrote in message <i1g51j$5g4$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i1g240$rho$1@fred.mathworks.com>...
> > Oops! I meant
> >
> > x = a+(b-a)*betarnd((c-a)/(b-a),(b-c)/(b-a),m,n);
> >
> > Roger Stafford
>
> Thanks Roger that is exactly what I needed!
>
> Corey Adrian

Subject: Random Number Generation

From: Barry Williams

Date: 26 Jun, 2013 19:08:07

Message: 7 of 8

"Steven Lord" <slord@mathworks.com> wrote in message <i1fib6$eso$1@fred.mathworks.com>...
>
> "Corey Adrian" <uryguitarist777@yahoo.com> wrote in message
> news:i1fhko$rsc$1@fred.mathworks.com...
> >I am trying to create a random number array given a minimum, maximum, and
> >mean value. Is there any way to do this?
>
> Sure, just write a function that errors if the specified mean value is less
> than the specified minimum or greater than the specified maximum value and
> otherwise returns your mean value.
>
> http://dilbert.com/strips/comic/2001-10-25/
>
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

Long, tiring day. My brain is hurting. Thanks for the smiles in that link.
:-)
Barry

Subject: Random Number Generation

From: Roger Stafford

Date: 27 Jun, 2013 05:23:06

Message: 8 of 8

"taffi" wrote in message <kqf3he$rv4$1@newscl01ah.mathworks.com>...
> i need to use a std deviation as well. i have a max, min, mean and also a std value. How can i get random numbers of beta distribution wid a certain standard deviation.
-------------------
  As I said back in 2010. "You can multiply the first two arguments of betarnd by any common factor to change the distribution's variance at your pleasure without affecting the mean." This can be done as follows. Call a = max, b = min, m = mean, and s = std. Then you can generate the desired beta distribution with

 f = ((b-m)*(m-a)-s^2)/s^2;
 x = a+(b-a)*betarnd((m-a)/(b-a)*f,(b-m)/(b-a)*f,M,N);

As you can see, that "common factor" is the above 'f'. Note that both m and s are constrained by the inequalities

 a < m < b
 0 < s < sqrt((b-m)*(m-a))

so the choice of s isn't fully "at your pleasure".

  You can consult the website

 http://en.wikipedia.org/wiki/Beta_distribution

to check that the above formula results in the desired mean and standard deviation.

Roger Stafford

Tags for this Thread

No tags are associated with 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