about rand

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xcm

    #1

    about rand



    #include <stdlib.h>

    static unsigned long int next = 1;

    int rand(void)
    {
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/(2 * (RAND_MAX +1L)) % (RAND_MAX+1L));
    }

    void srand(unsigned int seed)
    {
    next = seed;
    }




    next = next * 1103515245 + 12345;


    1103515245,1234 5
    why not 1103511234 ,54321?




  • lawrence.jones@ugs.com

    #2
    Re: about rand

    xcm <kukucm@163.com > wrote:[color=blue]
    >
    > 1103515245,1234 5
    > why not 1103511234 ,54321?[/color]

    Because 1103515245 and 12345 were the numbers chosen by whoever wrote
    the original Unix implementation back in the dark ages and thus have
    been used by implementers ever since for the sake of compatibility.

    -Larry Jones

    I think grown-ups just ACT like they know what they're doing. -- Calvin

    Comment

    Working...