Re: Random string
From: Jerry Coffin (jcoffin_at_taeus.us)
Date: 10/30/04
- Next message: Vince: "Visual Studio .NET , MFC 71 and Truncate"
- Previous message: ak: "Re: default font in a MFC program (main menu)"
- In reply to: Chiap Zap: "Re: Random string"
- Next in thread: BekTek: "Re: Random string"
- Messages sorted by: [ date ] [ thread ]
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.
- Next message: Vince: "Visual Studio .NET , MFC 71 and Truncate"
- Previous message: ak: "Re: default font in a MFC program (main menu)"
- In reply to: Chiap Zap: "Re: Random string"
- Next in thread: BekTek: "Re: Random string"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|