|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Up Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Payments Humor Music |
DeSmet C random number related functionsThese are two functions in DeSmet C related to random numbers.
|
/* ===========================================================*/
/* RANDOM NUMBER RELATED FNS */
/* Copyright (C)1994 Steven Whitney. Published under the */
/* GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/* Initially published by http://25yearsofprogramming.com. */
/* ===========================================================*/
/* -------------------------------------------------------------- */
/* SEEDRAND.C 5-10-94 AUTOMATICALLY SEEDS RANDOM NUMBER GENERATOR WITH A
NUMBER UNLIKELY TO BE REPEATED. MAKES USER-PROVIDED SEED UNNECESSARY.
*/
/* -------------------------------------------------------------- */
int seedrand()
{
int i;
char buf[9];
void times(), srand();
long timesecs();
times(buf); /* INT CONVERSION THROWS AWAY HIGH 2 BYTES */
i = (int)(timesecs(buf)); /* LEAVING ANY VALUE FROM -32768 TO 32767 */
srand(i);
return i; /* RETURN SEED IN CASE USED FOR SOMETHING */
}
/* -------------------------------------------------------------- */
/* POSRND() 2-19-94 */
/* RETURNS A RANDOM NUMBER FROM = 0. TO < 1.0 */
/* NECESSARY BECAUSE OF A BUG IN DESMET'S FRAND(). It sometimes returns an out of range number */
/* USE (INT)(POSRND() * N.) TO GET A RANDOM INTEGER FROM 0 TO N-1 */
/* -------------------------------------------------------------- */
double posrnd()
{
double r, frand();
do
{
r = frand();
}
while((r < 0.) || (r >= 1.));
return r;
}
|
|
|
|
|
Copyright ©2011 Steven Whitney. Last modified Tue 05/24/2011 12:27:03 -0700. |
||