Re: question about random generator



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, add-on 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 floating-point 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
normally-distributed 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 high-precision work you
should substitute a source whose characteristics you can
count on.)

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Alternative rand()-algorithm?
    ... > rand() works okay for me, but I suppose it has its limitations? ... programming languages", if I remember correctly, he likes such problems, ... global objects but before main or before global objects and before main? ... My real email is formed by deleteing the letter combination ...
    (comp.lang.c)
  • Re: Constructing a random permutation on the fly
    ... This raises the problem, oft-solved on sci.crypt, of generating uniform trits from uniform bits. ... /* Return a random choice from the uniform distribution ... if (rand < target_range) ...
    (sci.crypt)
  • Re: Uniform Random Number Generator
    ... I am not sure what rand does in ruby and I can't seem to find it online ... My initial assumption is that it follows a uniform distribution, ... My blog on Ruby ...
    (comp.lang.ruby)
  • Re: how to access the file?
    ... Abhay wrote: ... (if my memory doent fail me). ... RAND Uniformly distributed pseudo-random numbers. ... drawn from a uniform distribution on the unit interval. ...
    (comp.soft-sys.matlab)
  • Re: how to generate random char in C programming?
    ... int n = rand% 26; ... Is it possible to do it in C programming? ... rand() exists in C as well - in fact, it is one of the many library ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)