About rand()

Discussion in 'C Programming' started by Spiros Bousbouras, Jan 16, 2007.

  1. The standard says that rand() should return a pseudo-random
    number but what does pseudorandom mean ? If an implementation
    of rand() always returned the same number would it be conforming ?
    What if it always alternated between 2 values ?

    On the practical side do you have any thoughts on what one
    could realistically expect from the behaviour of rand() ? Could
    for example one expect that eventually any value in the range
    [0,RAND_MAX] will be returned ?
     
    Spiros Bousbouras, Jan 16, 2007
    #1
    1. Advertising

  2. Spiros Bousbouras wrote:
    > The standard says that rand() should return a pseudo-random
    > number but what does pseudorandom mean ? If an implementation
    > of rand() always returned the same number would it be conforming ?
    > What if it always alternated between 2 values ?
    >
    > On the practical side do you have any thoughts on what one
    > could realistically expect from the behaviour of rand() ? Could
    > for example one expect that eventually any value in the range
    > [0,RAND_MAX] will be returned ?


    It's entirely a QoI issue. Pragmatically, it is trivial to write a
    rand()
    that returns every value at some point, even if the distribution is
    not perfect. [A few implementations I've seen copy the example
    shown in K&R2.]

    The FAQ has a number of comments on the issues of rand().

    Anyone needing to use random numbers in a serious program will
    likely roll their own routine from the plethora of PRNG's available on
    the net. [E.g. http://www.stanford.edu/~blp/writings/clc/random.html.]

    --
    Peter
     
    Peter Nilsson, Jan 16, 2007
    #2
    1. Advertising

  3. Peter Nilsson wrote:
    > Spiros Bousbouras wrote:
    > > The standard says that rand() should return a pseudo-random
    > > number but what does pseudorandom mean ? If an implementation
    > > of rand() always returned the same number would it be conforming ?
    > > What if it always alternated between 2 values ?
    > >
    > > On the practical side do you have any thoughts on what one
    > > could realistically expect from the behaviour of rand() ? Could
    > > for example one expect that eventually any value in the range
    > > [0,RAND_MAX] will be returned ?

    >
    > It's entirely a QoI issue. Pragmatically, it is trivial to write a
    > rand()
    > that returns every value at some point, even if the distribution is
    > not perfect. [A few implementations I've seen copy the example
    > shown in K&R2.]


    By "perfect" distribution do you mean uniform distribution ?
     
    Spiros Bousbouras, Jan 16, 2007
    #3
  4. In article <>,
    Spiros Bousbouras <> wrote:
    >The standard says that rand() should return a pseudo-random
    >number but what does pseudorandom mean ? If an implementation
    >of rand() always returned the same number would it be conforming ?
    >What if it always alternated between 2 values ?


    It must produce a pseudo-random sequence in the range 0
    to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
    implementation it can never produce RAND_MAX then RAND_MAX
    for that implementation would be defined as the largest value that
    it -could- produce, and if that value was not at least 32767 then
    the implementation would be non-conforming.

    One could similarily argue that if 0 cannot be produced that
    the implementation is non-conforming; the argument is perhaps
    a bit weaker.

    If the implementation alternated between 0 and RAND_MAX then
    it would be conforming.


    >On the practical side do you have any thoughts on what one
    >could realistically expect from the behaviour of rand() ? Could
    >for example one expect that eventually any value in the range
    >[0,RAND_MAX] will be returned ?


    I would not -expect- any self-respecting rand() to not be
    able to produce one of the values in the range, eventually;
    I would -expect- at worst the sample function given in the
    standard. But it wouldn't shock me if some organization
    that produced a C-like language used what they -thought-
    was a good rand() but which turned out not to be able to produce
    some set of values. [NB: the number of organizations that
    produce C-like languages appears to far outnumber the ones that
    produce conforming C.]

    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
     
    Walter Roberson, Jan 16, 2007
    #4
  5. Walter Roberson wrote:
    > In article <>,
    > Spiros Bousbouras <> wrote:
    > >The standard says that rand() should return a pseudo-random
    > >number but what does pseudorandom mean ? If an implementation
    > >of rand() always returned the same number would it be conforming ?
    > >What if it always alternated between 2 values ?

    >
    > It must produce a pseudo-random sequence in the range 0
    > to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
    > implementation it can never produce RAND_MAX then RAND_MAX
    > for that implementation would be defined as the largest value that
    > it -could- produce, and if that value was not at least 32767 then
    > the implementation would be non-conforming.


    The standard says "The rand function computes a
    sequence of pseudo-random numbers in the range
    of 0 to RAND_MAX." I don't see how it follows from
    that that the largest value which can be produced
    will by definition be RAND_MAX.
     
    Spiros Bousbouras, Jan 16, 2007
    #5
  6. In article <>,
    Spiros Bousbouras <> wrote:

    >The standard says "The rand function computes a
    >sequence of pseudo-random numbers in the range
    >of 0 to RAND_MAX." I don't see how it follows from
    >that that the largest value which can be produced
    >will by definition be RAND_MAX.


    The standard is rather weak in its description of rand(). It makes
    no mention of the distribution of random numbers, so an implementation
    that returns 1 99.99999999% of the time could still be conforming.
    Presumably this would be considered a quality of implementation issue,
    and the same goes for an implementation that never produced RAND_MAX.

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jan 16, 2007
    #6
  7. Spiros Bousbouras

    user923005 Guest

    Spiros Bousbouras wrote:
    > Walter Roberson wrote:
    > > In article <>,
    > > Spiros Bousbouras <> wrote:
    > > >The standard says that rand() should return a pseudo-random
    > > >number but what does pseudorandom mean ? If an implementation
    > > >of rand() always returned the same number would it be conforming ?
    > > >What if it always alternated between 2 values ?

    > >
    > > It must produce a pseudo-random sequence in the range 0
    > > to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
    > > implementation it can never produce RAND_MAX then RAND_MAX
    > > for that implementation would be defined as the largest value that
    > > it -could- produce, and if that value was not at least 32767 then
    > > the implementation would be non-conforming.

    >
    > The standard says "The rand function computes a
    > sequence of pseudo-random numbers in the range
    > of 0 to RAND_MAX." I don't see how it follows from
    > that that the largest value which can be produced
    > will by definition be RAND_MAX.


    According to that definition the sequence:
    1,1,1,1...
    seems to fit because 1 is between 0 and RAND_MAX.

    The rand() function cannot produce negative numbers (by definition)
    The rand() function cannot produce numbers larger than RAND_MAX (by
    definition).

    Generally speaking, a halfway-decent rand() implementation will
    eventually produce every number between and including 0 .. RAND_MAX.

    However, there is not guarantee in the standard that rand() is halfway
    decent (e.g. that it passes Marsaglia's tests).

    If you want good pseudo-random numbers, then use the Mersenne Twister.
     
    user923005, Jan 16, 2007
    #7
  8. "Spiros Bousbouras" <> wrote in message
    news:...
    > The standard says that rand() should return a pseudo-random
    > number but what does pseudorandom mean ? If an implementation
    > of rand() always returned the same number would it be conforming ?
    > What if it always alternated between 2 values ?
    >
    > On the practical side do you have any thoughts on what one
    > could realistically expect from the behaviour of rand() ? Could
    > for example one expect that eventually any value in the range
    > [0,RAND_MAX] will be returned ?
    >


    Pseudo-random means that the next value of the output is a function of some
    internal state. Often, the state and the returned value are the same thing.
    Pseudo-random means that the next value is algorithmically predictable, i.e.
    it isn't truly random where one could not predict.

    http://en.wikipedia.org/wiki/Pseudo-random

    The most common approach to pseudo-random generators is to use prime moduli.

    http://en.wikipedia.org/wiki/Linear_congruential_generator

    In fact, most people who implement their own random number generators are
    pretty happy with the simple one at the link immediately above. I believe
    as long as the requirement of coprimality between a couple of the parameters
    is met, the generated sequence is guaranteed to hit every value in the
    interval. I'm sure all the math is at the link above.

    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Jan 17, 2007
    #8
  9. Spiros Bousbouras

    CBFalconer Guest

    "David T. Ashley" wrote:
    >

    .... snip ...
    >
    > In fact, most people who implement their own random number
    > generators are pretty happy with the simple one at the link
    > immediately above. I believe as long as the requirement of
    > coprimality between a couple of the parameters is met, the
    > generated sequence is guaranteed to hit every value in the
    > interval. I'm sure all the math is at the link above.


    Just read Knuth (TAOCP). He has a thorough treatment of linear
    congruential generators, including some cookbook tables.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Jan 17, 2007
    #9
  10. "CBFalconer" <> wrote in message
    news:...
    > "David T. Ashley" wrote:
    >>

    > ... snip ...
    >>
    >> In fact, most people who implement their own random number
    >> generators are pretty happy with the simple one at the link
    >> immediately above. I believe as long as the requirement of
    >> coprimality between a couple of the parameters is met, the
    >> generated sequence is guaranteed to hit every value in the
    >> interval. I'm sure all the math is at the link above.

    >
    > Just read Knuth (TAOCP). He has a thorough treatment of linear
    > congruential generators, including some cookbook tables.


    Thanks. I have those books on my shelves and actually didn't know that
    random number generation was in it (I've used it most for extended-precision
    arithmetic and searching and sorting).

    I've always admired Knuth. He has a mathematician's gift for brevity.
    There is a lot packed into those books.

    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Jan 17, 2007
    #10
  11. Spiros Bousbouras

    Spoon Guest

    Walter Roberson wrote:

    > Spiros Bousbouras wrote:
    >
    >> The standard says that rand() should return a pseudo-random
    >> number but what does pseudorandom mean ? If an implementation
    >> of rand() always returned the same number would it be conforming ?
    >> What if it always alternated between 2 values ?

    >
    > It must produce a pseudo-random sequence in the range 0
    > to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
    > implementation it can never produce RAND_MAX then RAND_MAX
    > for that implementation would be defined as the largest value that
    > it -could- produce, and if that value was not at least 32767 then
    > the implementation would be non-conforming.
    >
    > One could similarily argue that if 0 cannot be produced that
    > the implementation is non-conforming; the argument is perhaps
    > a bit weaker.


    The sequence
    u{n+1} = M * u{n} MOD C
    with M=16807 and C=2^31-1
    is often used as a PRNG.

    It does not produce 0. (Otherwise it would always return 0.)

    glibc's rand() implementation does not produce 0.

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    unsigned long i = 1;
    for (i=1; i; ++i)
    {
    if (rand() == 0) puts("rand() == 0");
    }
    return 0;
    }

    $ /usr/bin/time ./a.out
    161.65user 0.08system 2:56.68elapsed 91%CPU


    > If the implementation alternated between 0 and RAND_MAX then
    > it would be conforming.
    >
    >
    >> On the practical side do you have any thoughts on what one
    >> could realistically expect from the behaviour of rand() ? Could
    >> for example one expect that eventually any value in the range
    >> [0,RAND_MAX] will be returned ?

    >
    > I would not -expect- any self-respecting rand() to not be
    > able to produce one of the values in the range, eventually;
    > I would -expect- at worst the sample function given in the
    > standard. But it wouldn't shock me if some organization
    > that produced a C-like language used what they -thought-
    > was a good rand() but which turned out not to be able to produce
    > some set of values. [NB: the number of organizations that
    > produce C-like languages appears to far outnumber the ones that
    > produce conforming C.]


    Regards.
     
    Spoon, Jan 17, 2007
    #11
  12. Spiros Bousbouras

    Spoon Guest

    Spoon wrote:

    > glibc's rand() implementation does not produce 0.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > int main(void)
    > {
    > unsigned long i = 1;
    > for (i=1; i; ++i)
    > {
    > if (rand() == 0) puts("rand() == 0");
    > }
    > return 0;
    > }
    >
    > $ /usr/bin/time ./a.out
    > 161.65user 0.08system 2:56.68elapsed 91%CPU


    Doh! This only means that glibc's rand() does not return 0 after 2^32
    invocations. Not that it never returns 0.
     
    Spoon, Jan 17, 2007
    #12
  13. Spiros Bousbouras

    Spoon Guest

    Spoon wrote:

    > Spoon wrote:
    >
    >> glibc's rand() implementation does not produce 0.
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> int main(void)
    >> {
    >> unsigned long i = 1;
    >> for (i=1; i; ++i)
    >> {
    >> if (rand() == 0) puts("rand() == 0");
    >> }
    >> return 0;
    >> }
    >>
    >> $ /usr/bin/time ./a.out
    >> 161.65user 0.08system 2:56.68elapsed 91%CPU

    >
    > Doh! This only means that glibc's rand() does not return 0 after 2^32
    > invocations. Not that it never returns 0.


    #include <stdlib.h>
    int main(void)
    {
    while (rand() != 0);
    return 0;
    }

    $ /usr/bin/time ./a.out
    181.70user 0.03system 3:05.70elapsed 97%CPU

    I stand corrected.

    What is true is that linear congruential generators with B=0 obviously
    never output 0.

    http://en.wikipedia.org/wiki/Linear_congruential_generator
     
    Spoon, Jan 17, 2007
    #13
  14. "Spiros Bousbouras" <> wrote in message
    > The standard says that rand() should return a pseudo-random
    > number but what does pseudorandom mean ? If an implementation
    > of rand() always returned the same number would it be conforming ?
    > What if it always alternated between 2 values ?
    >

    Pseudorandom means that it passes or nearly passes a lot of tests for
    randomness, but is in fact deterministic.
    When you say "would a rand() that always returned the same number/
    alternated between 2 values be conforming?" really you are asking a
    meaningless question. It just depends on the precise English-language
    sentence used in the standard. There are no requirements for RNGs other than
    that they be RNGs.
    >
    > On the practical side do you have any thoughts on what one
    > could realistically expect from the behaviour of rand() ? Could
    > for example one expect that eventually any value in the range
    > [0,RAND_MAX] will be returned ?
    >

    A decent implementation will do this, but you cannot rely on it. The numbers
    will be very roughly uniformly distributed throughout the range, but the
    lower bits are allowed to cycle.
    Hence

    x * rand()/(RAND_MAX + 1.0)

    rather than

    rand() % x;
     
    Malcolm McLean, Jan 18, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niko D. Barli

    rand function in Modelsim 5.7c

    Niko D. Barli, Aug 26, 2004, in forum: VHDL
    Replies:
    9
    Views:
    6,421
    Niko D. Barli
    Sep 6, 2004
  2. daniel kaplan

    rand() question

    daniel kaplan, Sep 15, 2004, in forum: Perl
    Replies:
    4
    Views:
    642
    Ian Sedwell
    Sep 21, 2004
  3. Amelyan

    what is rand?

    Amelyan, Mar 31, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    560
    Kevin Spencer
    Mar 31, 2006
  4. Orhan Demirel

    usage of rand()

    Orhan Demirel, Jul 21, 2003, in forum: C++
    Replies:
    1
    Views:
    409
    Adam Fineman
    Jul 21, 2003
  5. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    258
    Morton Goldberg
    Sep 16, 2007
Loading...

Share This Page