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: Harish

Date: 5 Apr, 2010 02:37:07

Message: 1 of 8

How to generate random numbers in the interval [0 1] ?

Subject: random number generation

From: Steven Lord

Date: 5 Apr, 2010 02:49:01

Message: 2 of 8


"Harish " <harish.campus@gmail.com> wrote in message
news:hpbicj$rrl$1@fred.mathworks.com...
> How to generate random numbers in the interval [0 1] ?

Assuming you want to use the uniform distribution, use RAND. If you want
some other distribution, look at the random number generating functions in
Statistics Toolbox.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: random number generation

From: ImageAnalyst

Date: 5 Apr, 2010 03:20:09

Message: 3 of 8

Harish should also be aware that there is a help facility available in
MATLAB, where this kind of basic question would have been answered
very very promptly, much quicker than asking via the newsgroup. There
is a blue question mark icon, as well as a "Help" pulldown menu item
on the main menu. Perhaps Harish somehow missed them, but they're
very useful to know about.

Subject: random number generation

From: Derek O'Connor

Date: 5 Apr, 2010 05:47:03

Message: 4 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <72c7b342-e781-44cd-986e-91d68c0808c6@x38g2000vbx.googlegroups.com>...
> Harish should also be aware that there is a help facility available in
> MATLAB, where this kind of basic question would have been answered
> very very promptly, much quicker than asking via the newsgroup. There
> is a blue question mark icon, as well as a "Help" pulldown menu item
> on the main menu. Perhaps Harish somehow missed them, but they're
> very useful to know about.

--------

Let's not be too Harsh on Harish.

Does RAND return a floating point number in (0,1), (0,1], [0,1), or [0,1]?

What seed(state) for RAND will give 0 or 1?

>>help rand gave me no help on this question.

Derek O'Connor.

Subject: random number generation

From: Peter Perkins

Date: 5 Apr, 2010 13:27:03

Message: 5 of 8

On 4/5/2010 1:47 AM, Derek O'Connor wrote:

> Does RAND return a floating point number in (0,1), (0,1], [0,1), or [0,1]?
>
> What seed(state) for RAND will give 0 or 1?
>
>>> help rand gave me no help on this question.

In the current release,

>> help rand
  RAND Uniformly distributed pseudorandom numbers.
     R = RAND(N) returns an N-by-N matrix containing pseudorandom values drawn
     from the standard uniform distribution on the open interval(0,1).
[snip]

Subject: random number generation

From: Derek O'Connor

Date: 5 Apr, 2010 14:50:22

Message: 6 of 8

Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <hpcof7$2sp$1@fred.mathworks.com>...
> On 4/5/2010 1:47 AM, Derek O'Connor wrote:
>
> > Does RAND return a floating point number in (0,1), (0,1], [0,1), or [0,1]?
> >
> > What seed(state) for RAND will give 0 or 1?
> >
> >>> help rand gave me no help on this question.
>
> In the current release,
>
> >> help rand
> RAND Uniformly distributed pseudorandom numbers.
> R = RAND(N) returns an N-by-N matrix containing pseudorandom values drawn
> from the standard uniform distribution on the open interval(0,1).
> [snip]
----------------------------------------------------------------------

Thanks Peter. My R2008 <help rand> says:
> R = RAND(N) returns an N-by-N matrix containing pseudo-random values
> drawn from a uniform distribution on the unit interval.
>
> However, when I read further down I see 3 items:
>
> 1. This method ('twister') generates double precision values in the closed interval [2^(-53), 1-2^(-53)], with a period of (2^19937-1)/2.
>
> 2. This method ('state') generates double precision values in the closed interval [2^(-53), 1-2^(-53)], with a period of over 2^1492.
>
> 3. This method ('seed') generates double precision values in the closed interval [1/(2^31-1), 1-1/(2^31-1)], with a period of 2^31-2.

So, 'twister' and state generate DP numbers in [eps/2, 1-eps/2] which gives

>> disp([eps/2; 1-eps/2]) % MT and State
    1.110223024625157e-016
    9.999999999999999e-001

Seed gives
    
>> disp([1/(2^31-1); 1-1/(2^31-1)]) % Seed
    4.656612875245797e-010
    9.999999995343387e-001

This may be a dumb question, but I'll ask it anyway: can rand's output be modified so that 0 and 1 appear as two extra pseudo-random numbers? Remember, Harish wants [0,1].

Derek O'Connor.

Subject: random number generation

From: Peter Perkins

Date: 5 Apr, 2010 15:23:24

Message: 7 of 8

On 4/5/2010 10:50 AM, Derek O'Connor wrote:

> This may be a dumb question, but I'll ask it anyway: can rand's output
> be modified so that 0 and 1 appear as two extra pseudo-random numbers?
> Remember, Harish wants [0,1].

He may. Harish, you did say "in", but perhaps you meant "including the endpoints". There's no flag or setting you can use to get that. Most people do not want those endpoints, because they would mess up things like the inversion method to get a non-uniform distribution.

Subject: random number generation

From: Scott

Date: 20 Apr, 2012 22:33:17

Message: 8 of 8

"Harish " <harish.campus@gmail.com> wrote in message <hpbicj$rrl$1@fred.mathworks.com>...
> How to generate random numbers in the interval [0 1] ?

%% my_rand
% build in rand.m funciton in matlab is not inclusive on the interval 0 to
% 1, this is an attempt to correct this. This function will create a
% uniform distribution of peudio-random numbers [0,1].
%
% See also rand, randi

function r = my_rand(arr)
    p = 10000; % precission
    r = randi(p,arr);
    r = (r-1) ./ (p-1);
   
    % testing results
    figure(); hist(r,100);
    size(r(r==1))
    size(r(r==0))
end

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