s-news
[Top] [All Lists]

Generating Random Real Numbers

To: splus list <s-news@wubios.wustl.edu>
Subject: Generating Random Real Numbers
From: Alastair Dempster <demp2979@mach1.wlu.ca>
Date: Wed, 04 Apr 2001 11:45:00 -0400
Organization: Wilfrid Laurier University
I would like to thank all of you for your fast and helpful
responses:

Simon Rosenfeld, Mike Sawada, Samuel Buttrey, Terry Cox, Duangporn
J., Don McKenzie, Dave Krantz, James Pratt, Gerry Blais, Rolf
Turner, Charles Berry, Charles Annis, Brian Ripley, Nick Ellis,
Peter Ellis, Alan Zaslavsky, Florin Vaida.  I hope I have not missed
anyone!

Alastair Dempster

My colleague replies:

As many have pointed out, a normal distribution is defined on the
real line and does not exist for a defined range.

Joseph Beyene suggested that I set the  mean to the midpoint of the
range and then take values lying within 4 standard deviations (Prof.
Ripley suggested 3) as follows:

Step-1: The Gaussian mean parameter should be located at the
        center of the support. Here the center is
       (0.005 + 0.035)/2 = 0.02 = mu;

Step-2: Determine sigma using the following simple algebra:
      P(0.005 < X < 0.035) = 1
P((0.005-0.02)/sigma < (X-0.02)/sigma < (0.035-0.02)/sigma) =1
P(-0.015/sigma < Z < 0.015/sigma) = 1, where Z is N(0,1).
But we also know that P(-4 < Z < 4) is ALMOST 1, so can set
4 = 0.015/sigma, which implies sigma = 0.00375.

Step-3:  We can now generate random samples from a N(0.02, 0.00375)
distribution in the usual way.
e.g.
 rnorm(n=10,mean=0.02,sd=0.00375)

Nick Ellis, Prof. Ripley and Alan Zaslavsky suggested sampling from
a truncated normal (rejection sampling).  Then the sample will be
from a distribution that is proportional to the normal distrbution
and zero outside, but itself will not be normal.  This summarized
suggestion in combination with Joseph Beyene?s seems the easiest to
implement for my purposes.

Thanks again to all who responded.


***Original Message***
I'm posting this for an Splus user who is not part of this
discussion group:

She writes:
"I am interested in generating a set of 100 random real numbers
between 0.005 and 0.035, where the probability of drawing a specific
value is defined by the normal distribution.  rnorm() will not let
me define the limits of my dataset, yet my model parameter values
cannot physically exceed 0.035 by definition.  Is anyone aware of
how to specify range limits when generating random numbers with a
non-uniform distribution? I have explored sample(), runif(),
rnorm(), and rmvnorm(), all of which nicely generate random
numbers.  However, either they are not real (as in the case of
sample()), or I cannot figure out how to define a range of values
for the random sample."

She is using Splus 2000 under NT4.

I have had a look at the discussions about random number generation
in the archives but did not find anything suitable.

Can anyone tell us how to code this or provide a reference?  I will
post a summary on behalf of my colleague.

Thank you,

Alastair Dempster
Wilfrid Laurier University
Waterloo, Canada







<Prev in Thread] Current Thread [Next in Thread>