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 functions

These are two functions in DeSmet C related to random numbers. 

  • Automatically seed the random number generator using the system time.
  • A bug fix wrapper for DeSmet frand(), which occasionally returns a negative number.
/* ===========================================================*/
/* 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;
}

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.