Re: Random string

From: Jerry Coffin (jcoffin_at_taeus.us)
Date: 10/30/04


Date: Sat, 30 Oct 2004 13:27:47 -0600

In article <DB1C00EA6D62A074.991A5B476DE02CC8@gopxqhdj.hkjqmbka>,
chiap-the-zap@kiuj-kavwqt.com says...

[ ... ]

> I couldn't find bad randomness, when I checked the
> rand()-function out some years ago, provided that the random
> seed was set far enough away from the function itself, so as
> early at the program start for example, and maybe also with
> timeconsuming events like keystrokes inbetween.

I suspect you're either mis-remembering things, or else something was
happening that you weren't properly taking into account.

The time (or other code) between a call to srand() and a subsequent
call to rand() makes no difference whatsoever unless 1) your
implementation of rand() is badly defective, or 2) some of that
intervening code also called srand() and/or rand().

The bottom line is simple: having called srand() with some particular
parameter, the sequence of results returned from rand() is
deterministic. If (just picking a semi-random number of my own) I
call srand(81450), the next output from rand() with any particular
generator is then fixed, and executing other code [that doesn't call
rand() or srand()] or waiting an arbitrary amount of time will have
absolutely NO effect whatsoever on the results returned by calling
rand().

[ ... ]

> Anyway. Your "google-Exercise" didn't help much, so if you have
> a *simple* algorithm for better randomness, please feel free to
> post it here. Thanks. :)

#include <stdlib.h>

int rand_lim(int limit) {
    int divisor = RAND_MAX/(limit+1);
    int retval;

    do {
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

int rand_lim(int lower, int upper) {
    int range = abs(upper-lower);

    return rand_lim(range)+lower;
}

-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.


Relevant Pages

  • Re: Hunt for rand and srand implementations ;)
    ... Also I tested knoppix's rand and srand.... ... rand and srand... ... void srand(unsigned int seed); ... rand retrieves the pseudorandom numbers that are generated. ...
    (sci.crypt)
  • Re: Floating point load-store behaviour.
    ... This should be "int main". ... calling srand(), you're interfering with the generator. ... You should call srand() exactly once, ...
    (comp.lang.c)
  • Re: Hunt for rand and srand implementations ;)
    ... probably have the same srand() and randroutines... ... // IBM AIX srand and rand implementation occurding to: ... int IBM_AIX_rand ...
    (sci.crypt)
  • Re: rand and srand
    ... value from rand() by 2001, and returns the remainder - so the number can ... The C99 standard wants and int. ... automatic void of course with just as parameters. ... So is there really a need to use srand? ...
    (comp.lang.c)
  • Re: Extremely poor performance crunching random numbers under PIV-FC5
    ... in a tiny dynamic lib. ... int fakerand{ ... rand and fake rand functions this way: ... glibc rand(), randomand random_rfunctions, or at least related ...
    (Fedora)