"Ian Abbott" <[EMAIL PROTECTED]> writes:

> On 27 Nov 2001, at 15:16, Hrvoje Niksic wrote:
> 
>> So, does anyone know about the portability of rand()?
> 
> It's in the ANSI/ISO C spec (ISO 9899). It's always been in UNIX 
> (or at least it's been in there since UNIX 7th Edition), and I 
> should think it's always been in the MS-DOS compilers, but I don't 
> have one handy at the moment.

I've now switched to using it.  If someone complains we can resurrect
the check and the dummy implementations.  The draft of ISO 9899 I have
even contains such an implementation:

    static unsigned long int next = 1;

    int rand(void)   // RAND_MAX assumed to be 32767
    {
            next = next * 1103515245 + 12345;
            return (unsigned int)(next/65536) % 32768;
    }

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

> It tends not to be very random in some implementations, but should
> be good enough to implement a random wait.

Agreed.  Besides, I'm using the technique recommended in _Numerical
Recipes in C_, as quoted in the Linux rand(3) man page:

    "If you want to generate a random integer between 1 and 10, you
    should always do it by using high-order bits, as in

           j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

    and never by anything resembling

           j=1+(rand() % 10);

    (which uses lower-order bits)."

Reply via email to