P: n/a

Hi all
I am new to this forum and to the c programming language.
If I understand, the random() function in C return numbers that follow a
uniform distribution U(0,1). Can somebody know how to generate a set of
random number that follow a normal distribution N(0,1) ? I am develloping
on power4 machine running AIX.
Thank you for your help  
Share this Question
P: n/a

Marc Dansereau wrote: Hi all
I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1).
There is no standard function in C called random(). The standard
pseudorandom function is rand(). The values it returns are not U(0,1).
It returns pseudorandom numbers (no distribution specified) in the
range 0 to RAND_MAX, where RAND_MAX must be at least 32767.
Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ?
You should *always* check a newsgroup's FAQ before posting a question.
The core list of randomnumber questions are 13.15 through 13.20. In
particular, see question 13.20 "How can I generate random numbers with a
normal or Gaussian distribution?"
<http://www.eskimo.com/~scs/Cfaq/q13.20.html>  
P: n/a

Marc Dansereau wrote: Hi all
I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX.
First, there is no random() function in the Standard C
library. If your system offers such a function, it is an
extra, bonus, addon extension to C, not part of C itself.
The rand() function generates uniformly distributed
integers in the range 0 <= rand() <= RAND_MAX <= 32767.
From this source, you can generate floatingpoint numbers in
the range 0 <= y < 1: `rand() / (RAND_MAX + 1.0)'. Note
that these values may have as little as fifteen bits'
"precision;" if you need more, you may need to combine
the results of multiple rand() calls as in
(rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0)
(Pedants take note: The ambiguity in the above expression
may be considered a benefit, under the circumstances.)
Now that you've got a source of (approximately) uniformly
distributed numbers in [0,1), there are several ways to produce
normallydistributed numbers. The easiest to program is surely
the "polar method" (Google is your friend); faster methods
exist if you're willing to do the extra work. See Knuth
"The Art of Computer Programming, Volume II: Seminumerical
Algorithms." (In fact, see Knuth anyhow: the Standard makes
almost no guarantees about the statistical "goodness" of the
rand() function, and if you're doing highprecision work you
should substitute a source whose characteristics you can
count on.)

Eric Sosman es*****@acmdotorg.invalid  
P: n/a

Martin Ambuhl wrote: Marc Dansereau wrote: ... Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ?
You should *always* check a newsgroup's FAQ before posting a question. The core list of randomnumber questions are 13.15 through 13.20. In particular, see question 13.20 "How can I generate random numbers with a normal or Gaussian distribution?" <http://www.eskimo.com/~scs/Cfaq/q13.20.html>
It might also be worth looking at... http://benpfaff.org/writings/clc/random.html

Peter  
P: n/a

Marc Dansereau wrote: If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX.
The random() function does not exist in ISO C. It does, however, include the
rand() function which returns an int ranging from 0 to RAND_MAX. There are
no random number functions in ISO C which return a floating point value. My
advice would be to use rand() and a function which implements a suitable
normal distribution curve.  
P: n/a

Eric Sosman wrote: ... The rand() function generates uniformly distributed integers in the range 0 <= rand() <= RAND_MAX <= 32767.
ITYM: 0 <= rand() <= RAND_MAX, where RAND_MAX >= 32767

Peter  
P: n/a

Peter Nilsson wrote: Eric Sosman wrote:
... The rand() function generates uniformly distributed integers in the range 0 <= rand() <= RAND_MAX <= 32767.
ITYM: 0 <= rand() <= RAND_MAX, where RAND_MAX >= 32767
Indeed, yes, that is exactly and precisely what I,
of course, meant. (Blbbbpppfft  I *hate* when I do that.)

Eric Sosman es*****@acmdotorg.invalid  
P: n/a

Peter Nilsson wrote: Martin Ambuhl wrote: Marc Dansereau wrote: > ... > Can somebody know how to generate a set of > random number that follow a normal distribution N(0,1) ?
You should *always* check a newsgroup's FAQ before posting a question. The core list of randomnumber questions are 13.15 through 13.20. In particular, see question 13.20 "How can I generate random numbers with a normal or Gaussian distribution?" <http://www.eskimo.com/~scs/Cfaq/q13.20.html>
It might also be worth looking at...
http://benpfaff.org/writings/clc/random.html
Thank you for pointing these sites !
I am trying this code. The problem is that the
for (;;)
{
double u1 = prng_get_double ();
double u2 = prng_get_double ();
v1 = 2.0 * u1  1.0;
v2 = 2.0 * u2  1.0;
s = v1 * v1 + v2 * v2;
if (s > limit && s < 1)
break;
}
might end after a long time if u1 and u2 are very small, this loop might
take a time that is too long for my application (fluid simulation).  
P: n/a

Marc Dansereau wrote: If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX.
Wrong newsgroup. Try comp.programming, which deals with
algorithms. Somebody is very likely to show you how to create a
gaussian. I thought the C FAQ also had a method, but I guess not,
since you would have looked there before asking.

Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!  
P: n/a

Marc Dansereau wrote: Peter Nilsson wrote:
Martin Ambuhl wrote: Marc Dansereau wrote: > ... > Can somebody know how to generate a set of > random number that follow a normal distribution N(0,1) ?
You should *always* check a newsgroup's FAQ before posting a question. The core list of randomnumber questions are 13.15 through 13.20. In particular, see question 13.20 "How can I generate random numbers with a normal or Gaussian distribution?" <http://www.eskimo.com/~scs/Cfaq/q13.20.html>
It might also be worth looking at...
http://benpfaff.org/writings/clc/random.html
Thank you for pointing these sites !
I am trying this code. The problem is that the
for (;;) { double u1 = prng_get_double (); double u2 = prng_get_double (); v1 = 2.0 * u1  1.0; v2 = 2.0 * u2  1.0; s = v1 * v1 + v2 * v2; if (s > limit && s < 1) break; }
might end after a long time if u1 and u2 are very small, this loop might take a time that is too long for my application (fluid simulation).
humm ... according to Knuth, this loop will be executed "1.27 times on the
average, with a standard deviation of 0.587" (Art of computer programming
2, p 117), wich is probably not bad. I beleve him, but still wonder if
there is a way to avoid performance degradation when the values of u1 and
u2 are very small.  
P: n/a

"Free Bird" <@> writes: Marc Dansereau wrote:
If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX.
The random() function does not exist in ISO C. It does, however, include the rand() function which returns an int ranging from 0 to RAND_MAX. There are no random number functions in ISO C which return a floating point value. My advice would be to use rand() and a function which implements a suitable normal distribution curve.
If you care about the quality of your random numbers, my advice would
be to avoid the rand() function. Many C implementations have rand()
functions that aren't particularly good.

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.  
P: n/a

Eric Sosman wrote: Peter Nilsson wrote:
Eric Sosman wrote:
... The rand() function generates uniformly distributed integers in the range 0 <= rand() <= RAND_MAX <= 32767.
ITYM: 0 <= rand() <= RAND_MAX, where RAND_MAX >= 32767
Indeed, yes, that is exactly and precisely what I, of course, meant. (Blbbbpppfft  I *hate* when I do that.
Does "pseudorandom" mean the same thing as "uniformly distributed" ?

pete  
P: n/a

pete wrote: Does "pseudorandom" mean the same thing as "uniformly distributed" ?
Completely OT, but anyway... No, pseudorandom means that it looks like
it's random but it really isn't. There is no way to generate trully
random numbers with a computer, everything you do is deterministic, but
you can generate sequences that look like they're random but that
aren't. Hence the term _pseudo_random.  
P: n/a

Antonio wrote: pete wrote: Does "pseudorandom" mean the same thing as "uniformly distributed" ?
Completely OT, but anyway... No, pseudorandom means that it looks like it's random but it really isn't. There is no way to generate trully random numbers with a computer, everything you do is deterministic, but you can generate sequences that look like they're random but that aren't. Hence the term _pseudo_random.
Yes, we know, you didn't say anything that wasn't completely obvious.
The C Standard specifies that rand() generates pseudorandom numbers,
the questions is whether a conforming implementation could generate a
series of normal distributed numbers via the rand() function or if the
term pseudorandom implies that the numbers must be generated with a
uniform distribution. I was wondering the same thing myself, I think
the intention is that the numbers be uniform but that may be debatable.
Robert Gamble  
P: n/a

Marc Dansereau wrote: Marc Dansereau wrote:
Peter Nilsson wrote:
Martin Ambuhl wrote:
Marc Dansereau wrote:
>... >Can somebody know how to generate a set of >random number that follow a normal distribution N(0,1) ?
You should *always* check a newsgroup's FAQ before posting a question. The core list of randomnumber questions are 13.15 through 13.20. In particular, see question 13.20 "How can I generate random numbers with a normal or Gaussian distribution?" <http://www.eskimo.com/~scs/Cfaq/q13.20.html>
It might also be worth looking at...
http://benpfaff.org/writings/clc/random.html
Thank you for pointing these sites !
I am trying this code. The problem is that the
for (;;) { double u1 = prng_get_double (); double u2 = prng_get_double (); v1 = 2.0 * u1  1.0; v2 = 2.0 * u2  1.0; s = v1 * v1 + v2 * v2; if (s > limit && s < 1) break; }
might end after a long time if u1 and u2 are very small, this loop might take a time that is too long for my application (fluid simulation).
humm ... according to Knuth, this loop will be executed "1.27 times on the average, with a standard deviation of 0.587" (Art of computer programming 2, p 117), wich is probably not bad. I beleve him, but still wonder if there is a way to avoid performance degradation when the values of u1 and u2 are very small.
What is `limit' and why do you test against it? It
looks pointless to me, serving only to increase the
expected number of iterations by reducing the probability
of "acceptance" from pi/4 to (1limit^2)pi/4. (It may
also perturb the results, making them nonnormal; I'm
not sufficiently motivated to work it out.)
Besides: This method was suggested because it's easy
to implement. As I wrote in my earlier response
[...] faster methods exist if you're willing to do the extra work.
.... and since you've apparently got Knuth ready to hand,
you're staring at explicit recipes for at least two other
methods  even more if you're looking at the third
edition. If it's speed you want, what's stopping you?

Eric Sosman es*****@acmdotorg.invalid  
P: n/a

pete wrote: Eric Sosman wrote:
Peter Nilsson wrote:
Eric Sosman wrote:
... The rand() function generates uniformly distributed integers in the range 0 <= rand() <= RAND_MAX <= 32767.
ITYM: 0 <= rand() <= RAND_MAX, where RAND_MAX >= 32767
Indeed, yes, that is exactly and precisely what I, of course, meant. (Blbbbpppfft  I *hate* when I do that.
Does "pseudorandom" mean the same thing as "uniformly distributed" ?
No. The Standard doesn't actually guarantee any particular
distribution from rand(). On the DeathStation 9000, rand()
returns 42 every time you call it.
I've always assumed that the Standard doesn't describe the
distribution because doing so would require a lot of fairly
deep mathematics; Knuth devotes thirtyfive pages to the topic
"What is a random sequence?" Developing a useful theory of
the randomness of finite sequences is (it seems) no simple
matter  certainly not a topic for the Standard to explicate.
So the Standard just says "pseudorandom," and we all understand
that it means "sort of uniformish in a sort of handwaving way,
noodge noodge wink wink."
Anyhow, that's how I've imagined the committee's intent.
The Rationale sheds no further light on the matter, either.

Eric Sosman es*****@acmdotorg.invalid  
P: n/a

On 20050725 19:11:02 0500, Marc Dansereau <bo*******@hotmail.com> said: Hi all
I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX.
Thank you for your help
If by normal you mean Gaussian, remember that a sum of white
distributions tends to a gaussian. Many other methods you can choose,
less naive than this.

Sensei <se******@tin.it>
cd /pub
more beer  
P: n/a

Robert Gamble wrote: Antonio wrote: pete wrote: Does "pseudorandom" mean the same thing as "uniformly distributed" ?
Completely OT, but anyway... No, pseudorandom means that it looks like it's random but it really isn't. There is no way to generate trully random numbers with a computer, everything you do is deterministic, but you can generate sequences that look like they're random but that aren't. Hence the term _pseudo_random.
Yes, we know, you didn't say anything that wasn't completely obvious. The C Standard specifies that rand() generates pseudorandom numbers, the questions is whether a conforming implementation could generate a series of normal distributed numbers via the rand() function or if the term pseudorandom implies that the numbers must be generated with a uniform distribution. I was wondering the same thing myself, I think the intention is that the numbers be uniform but that may be debatable.
It may be completely obvious to you, and to many people (including
myself), but it doesn't seem to be obvious to "pete", since he asked.
What I was trying to explain is that pseudorandom does not imply
anything about the distribution of the numbers. You may get
pseudorandom numbers that look like a uniform distribution, or
pseudorandom numbers that look like a poisson distribution, or a
gaussian distribution, or anything you want. In fact if you are able to
generate pseudorandom numbers with any distribution, you can operate
with them to obtain any other distribution you want.
On the topic of wether the standard requires the distribution to be
uniform. Well I don't know the standard by hard, but many people here
seem to have a copy of it, so it should be a simple matter to check it.
Every implementation I've seen of C and almost any other language/tool
generates pseudorandom number using a multiplicative seed, simply
becase it's a good enough method and requires relatively few
operations.
And finally, I don't know if an implementation that generated normally
distributed numbers with the rand() function would conform to the
standard, but it would be beyond foolish.  
P: n/a

Antonio wrote: Robert Gamble wrote: Antonio wrote: pete wrote: > Does "pseudorandom" mean the same thing as "uniformly distributed" ?
Completely OT, but anyway... No, pseudorandom means that it looks like it's random but it really isn't. There is no way to generate trully random numbers with a computer, everything you do is deterministic, but you can generate sequences that look like they're random but that aren't. Hence the term _pseudo_random.
Yes, we know, you didn't say anything that wasn't completely obvious. The C Standard specifies that rand() generates pseudorandom numbers, the questions is whether a conforming implementation could generate a series of normal distributed numbers via the rand() function or if the term pseudorandom implies that the numbers must be generated with a uniform distribution. I was wondering the same thing myself, I think the intention is that the numbers be uniform but that may be debatable.
It may be completely obvious to you, and to many people (including myself), but it doesn't seem to be obvious to "pete", since he asked. What I was trying to explain is that pseudorandom does not imply anything about the distribution of the numbers. You may get pseudorandom numbers that look like a uniform distribution, or pseudorandom numbers that look like a poisson distribution, or a gaussian distribution, or anything you want. In fact if you are able to generate pseudorandom numbers with any distribution, you can operate with them to obtain any other distribution you want.
On the topic of wether the standard requires the distribution to be uniform. Well I don't know the standard by hard, but many people here seem to have a copy of it, so it should be a simple matter to check it. Every implementation I've seen of C and almost any other language/tool generates pseudorandom number using a multiplicative seed, simply becase it's a good enough method and requires relatively few operations.
And finally, I don't know if an implementation that generated normally distributed numbers with the rand() function would conform to the standard, but it would be beyond foolish.
You are missing the point. The standard only states that the numbers
generated are "pseudorandom", pete knows this, I know this, anyone who
has a copy of the Standard knows this. He was specifically refering to
this wording in the Standard and asking if this requirement alone
implied a uniform disribution since the Standard does not discuss
distribution requirements.
Robert Gamble  
P: n/a

Robert Gamble wrote: Antonio wrote: Robert Gamble wrote: Antonio wrote: > pete wrote: > > Does "pseudorandom" mean the same thing as "uniformly distributed" ? > > Completely OT, but anyway... No, pseudorandom means that it looks like > it's random but it really isn't. There is no way to generate trully > random numbers with a computer, everything you do is deterministic, but > you can generate sequences that look like they're random but that > aren't. Hence the term _pseudo_random.
Yes, we know, you didn't say anything that wasn't completely obvious. The C Standard specifies that rand() generates pseudorandom numbers, the questions is whether a conforming implementation could generate a series of normal distributed numbers via the rand() function or if the term pseudorandom implies that the numbers must be generated with a uniform distribution. I was wondering the same thing myself, I think the intention is that the numbers be uniform but that may be debatable.
It may be completely obvious to you, and to many people (including myself), but it doesn't seem to be obvious to "pete", since he asked. What I was trying to explain is that pseudorandom does not imply anything about the distribution of the numbers. You may get pseudorandom numbers that look like a uniform distribution, or pseudorandom numbers that look like a poisson distribution, or a gaussian distribution, or anything you want. In fact if you are able to generate pseudorandom numbers with any distribution, you can operate with them to obtain any other distribution you want.
On the topic of wether the standard requires the distribution to be uniform. Well I don't know the standard by hard, but many people here seem to have a copy of it, so it should be a simple matter to check it. Every implementation I've seen of C and almost any other language/tool generates pseudorandom number using a multiplicative seed, simply becase it's a good enough method and requires relatively few operations.
And finally, I don't know if an implementation that generated normally distributed numbers with the rand() function would conform to the standard, but it would be beyond foolish.
You are missing the point. The standard only states that the numbers generated are "pseudorandom", pete knows this, I know this, anyone who has a copy of the Standard knows this. He was specifically refering to this wording in the Standard and asking if this requirement alone implied a uniform disribution since the Standard does not discuss distribution requirements.
Then I have already answered your question. No, in no way pseudorandom
implies a uniform distribution. If the point is wether the creators of
the standard had a uniform distribution in mind when they phrased the
standard, well I wasn't one of them and AFAIK neither were you, so we
could talk for ages and still get to no conclusion.
If the standard just says that rand() generates pseudorandom integer
numbers between 0 and RAND_MAX >= 32767, then it says that, and only
that. If no reference to a certain distribution is made any
distribuiton would be valid. Of course the requirement that the values
must be within a certain range rules out lots of distributions
(poisson, gauss, Studentt, chi^2, cauchy...). So, from my POV, a
rand() function that returned 0 with p probability and 32767 with (1 
p) probability would conform to the standard... while beeing almost
totally useless.  
P: n/a

Eric Sosman <es*****@acmdotorg.invalid> writes: pete wrote:
[...] Does "pseudorandom" mean the same thing as "uniformly distributed" ?
No. The Standard doesn't actually guarantee any particular distribution from rand(). On the DeathStation 9000, rand() returns 42 every time you call it.
I've always assumed that the Standard doesn't describe the distribution because doing so would require a lot of fairly deep mathematics; Knuth devotes thirtyfive pages to the topic "What is a random sequence?" Developing a useful theory of the randomness of finite sequences is (it seems) no simple matter  certainly not a topic for the Standard to explicate. So the Standard just says "pseudorandom," and we all understand that it means "sort of uniformish in a sort of handwaving way, noodge noodge wink wink."
Anyhow, that's how I've imagined the committee's intent. The Rationale sheds no further light on the matter, either.
The Standard says that rand() "computes a sequence of pseudorandom
integers in the range 0 to RAND_MAX". Though it doesn't explicitly
say how they're distributed, I don't think anything other than a
uniform distribution would make any sense. If a normal distribution
were intended, integers in the range 0 to RAND_MAX would be an odd way
to specify it.
Also, the sample implementation yields a more or less uniform
distribution.
It's debatable (but *not* usefully debatable!) whether the DS9K's
implementation of rand() is conforming. It would have been nice,
IMHO, to have a footnote that mentions a uniform distribution, however
handwavingly.

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.  
P: n/a

Antonio wrote: Robert Gamble wrote: Antonio wrote: pete wrote: > Does "pseudorandom" mean the same thing as "uniformly > distributed" ?
Completely OT, but anyway... No, pseudorandom means that it looks like it's random but it really isn't. There is no way to generate trully random numbers with a computer, everything you do is deterministic,
There are entropy (et al) devices that generate numbers that are, to
all intents and purposes, truly random. However, they cannot be used to
implement rand() since the sequence generated must be repeatable
(if the programmer sets the same seed as before.)
but you can generate sequences that look like they're random but that aren't. Hence the term _pseudo_random.
Yes, we know, you didn't say anything that wasn't completely obvious. ...
It may be completely obvious to you, and to many people (including myself), but it doesn't seem to be obvious to "pete", since he asked.
I think pete's question to Eric was somewhat rhetorical, though perhaps
he too asked on behalf of those not inclined to ask (or know to ask.)
;)

Peter   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2526
 replies: 21
 date asked: Nov 15 '05
