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:
Specific range for a random number generator

Subject: Specific range for a random number generator

From: Chris Lee

Date: 21 Sep, 2007 10:50:37

Message: 1 of 9

I am new to Matlab and I'm looking to generate random
numbers with a gaussian distribution in an range of -1 to 1
with a mean of 0. The 'randn' function give me the desired
distribution, but I am unable to determine how to specify
the range. Any help would be greatly appreciated.

Subject: Specific range for a random number generator

From: Adam

Date: 21 Sep, 2007 12:00:53

Message: 2 of 9

"Chris Lee" <mitzoff@hotmail.com> wrote in message
<fd07lt$1rr$1@fred.mathworks.com>...
> I am new to Matlab and I'm looking to generate random
> numbers with a gaussian distribution in an range of -1 to 1
> with a mean of 0. The 'randn' function give me the desired
> distribution, but I am unable to determine how to specify
> the range. Any help would be greatly appreciated.

A Gaussian distribution by definition has a distribution of
(-inf, inf). The standard deviation (sigma) is 1 by default.

If you're looking for a uniform distribution over [-1, 1]
2*rand()-1

~Adam

Subject: Specific range for a random number generator

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Sep, 2007 16:02:17

Message: 3 of 9

In article <fd0bpl$8do$1@fred.mathworks.com>,
Adam <not.my.email@mathworks.com> wrote:
>"Chris Lee" <mitzoff@hotmail.com> wrote in message
><fd07lt$1rr$1@fred.mathworks.com>...
>> I am new to Matlab and I'm looking to generate random
>> numbers with a gaussian distribution in an range of -1 to 1
>> with a mean of 0. The 'randn' function give me the desired
>> distribution, but I am unable to determine how to specify
>> the range. Any help would be greatly appreciated.

>A Gaussian distribution by definition has a distribution of
>(-inf, inf).

Right -- so all Chris needs to do is divide the returned value
of randn() by inf in order to scale the results to [-1,1] :)
--
  Prototypes are supertypes of their clones. -- maplesoft

Subject: Specific range for a random number generator

From: Randy Poe

Date: 21 Sep, 2007 16:34:51

Message: 4 of 9

On Sep 21, 6:50 am, "Chris Lee" <mitz...@hotmail.com> wrote:
> I am new to Matlab and I'm looking to generate random
> numbers with a gaussian distribution in an range of -1 to 1
> with a mean of 0. The 'randn' function give me the desired
> distribution, but I am unable to determine how to specify
> the range. Any help would be greatly appreciated.

As others have pointed out, a normal (gaussian) distribution
has an infinite range. If you want something with limited
range, you will have to modify the output of randn in
some way.

The most obvious is to throw out samples that don't
fall in your desired range.

You are also going to have to specify what standard
deviation you want. The output of randn has variance
1 so the standard deviation is 1.

               - Randy

Subject: Specific range for a random number generator

From: Greg Heath

Date: 23 Sep, 2007 16:32:35

Message: 5 of 9

On Sep 21, 12:34 pm, Randy Poe <poespam-t...@yahoo.com> wrote:
> On Sep 21, 6:50 am, "Chris Lee" <mitz...@hotmail.com> wrote:
>
> > I am new to Matlab and I'm looking to generate random
> > numbers with a gaussian distribution in an range of -1 to 1
> > with a mean of 0. The 'randn' function give me the desired
> > distribution, but I am unable to determine how to specify
> > the range. Any help would be greatly appreciated.
>
> As others have pointed out, a normal (gaussian) distribution
> has an infinite range. If you want something with limited
> range, you will have to modify the output of randn in
> some way.
>
> The most obvious is to throw out samples that don't
> fall in your desired range.
>
> You are also going to have to specify what standard
> deviation you want. The output of randn has variance
> 1 so the standard deviation is 1.

A finite sample might have mean ~ -0.00212 or + 0.00234
and it might have stdv ~ 0.806 or stdv ~ 1.203.

So, if you fiddle around, decide whether you are going
to base the fiddling on the population mean and var (0,1)
or the sample mean and var.

Hope this helps.

Greg

Subject: Specific range for a random number generator

From: Greg Heath

Date: 23 Sep, 2007 16:42:29

Message: 6 of 9

On Sep 23, 12:32 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Sep 21, 12:34 pm, Randy Poe <poespam-t...@yahoo.com> wrote:
>
>
>
>
>
> > On Sep 21, 6:50 am, "Chris Lee" <mitz...@hotmail.com> wrote:
>
> > > I am new to Matlab and I'm looking to generate random
> > > numbers with a gaussian distribution in an range of -1 to 1
> > > with a mean of 0. The 'randn' function give me the desired
> > > distribution, but I am unable to determine how to specify
> > > the range. Any help would be greatly appreciated.
>
> > As others have pointed out, a normal (gaussian) distribution
> > has an infinite range. If you want something with limited
> > range, you will have to modify the output of randn in
> > some way.
>
> > The most obvious is to throw out samples that don't
> > fall in your desired range.
>
> > You are also going to have to specify what standard
> > deviation you want. The output of randn has variance
> > 1 so the standard deviation is 1.
>
> A finite sample might have mean ~ -0.00212 or + 0.00234
> and it might have stdv ~ 0.806 or stdv ~ 1.203.
>
> So, if you fiddle around, decide whether you are going
> to base the fiddling on the population mean and var (0,1)
> or the sample mean and var.
>
> Hope this helps.
>
> Greg- Hide quoted text -
>
> - Show quoted text -

Also remember that

1. Although the population mean of a truncated Gaussian distribution
is still 0, the population variance depends on the truncation
threshold.
2. The stats of a finite sample will differ from those of the
population.

Sorry for being so pedantic. However, it is easy to overlook details
when you are feverishly tring to make that deadline.

Greg

Subject: Specific range for a random number generator

From: mahesh kolte

Date: 15 Oct, 2007 02:32:34

Message: 7 of 9

"Adam " <not.my.email@mathworks.com> wrote in message
<fd0bpl$8do$1@fred.mathworks.com>...
> "Chris Lee" <mitzoff@hotmail.com> wrote in message
> <fd07lt$1rr$1@fred.mathworks.com>...
> > I am new to Matlab and I'm looking to generate random
> > numbers with a gaussian distribution in an range of -1 to 1
> > with a mean of 0. The 'randn' function give me the desired
> > distribution, but I am unable to determine how to specify
> > the range. Any help would be greatly appreciated.
>
> A Gaussian distribution by definition has a distribution of
> (-inf, inf). The standard deviation (sigma) is 1 by
default.
>
> If you're looking for a uniform distribution over [-1, 1]
> 2*rand()-1
>
> ~Adam

Subject: Specific range for a random number generator

From: Kwon Kuan Cheong

Date: 4 Apr, 2008 07:11:02

Message: 8 of 9

"mahesh kolte" <maheshkolte@mathwoks.com> wrote in message
<feujg2$doa$1@fred.mathworks.com>...
> "Adam " <not.my.email@mathworks.com> wrote in message
> <fd0bpl$8do$1@fred.mathworks.com>...
> > "Chris Lee" <mitzoff@hotmail.com> wrote in message
> > <fd07lt$1rr$1@fred.mathworks.com>...
> > > I am new to Matlab and I'm looking to generate random
> > > numbers with a gaussian distribution in an range of -
1 to 1
> > > with a mean of 0. The 'randn' function give me the
desired
> > > distribution, but I am unable to determine how to
specify
> > > the range. Any help would be greatly appreciated.
> >
> > A Gaussian distribution by definition has a
distribution of
> > (-inf, inf). The standard deviation (sigma) is 1 by
> default.
> >
> > If you're looking for a uniform distribution over [-1,
1]
> > 2*rand()-1
> >
> > ~Adam
>
Hi, how to set the output N? lets say from 1K, increase to
1M. Thanks

Subject: Specific range for a random number generator

From: Universiti Sains Malaysia

Date: 9 Aug, 2011 09:53:10

Message: 9 of 9

"Chris Lee" wrote in message <fd07lt$1rr$1@fred.mathworks.com>...
> I am new to Matlab and I'm looking to generate random
> numbers with a gaussian distribution in an range of -1 to 1
> with a mean of 0. The 'randn' function give me the desired
> distribution, but I am unable to determine how to specify
> the range. Any help would be greatly appreciated.

Dear reply to your question

function X=random_generator(n, x_max,x_min)
    X=[x_min+((rand(n,n)).*(x_max-x_min))];
end

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