prand1.c

/*
 * example of a pseudo-rando number generator (PRNG)
 * the seed comes from the time of day (in seconds)
 *
 * Matt Bishop, ECS 36A
 * -- May 22, 2024 	original program
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
 * here we go!
 */
int main(void)
{
	int i;		/* counter in a for loop */
	time_t tick;	/* used to seed the PRNG */

	/*
	 * generate the seed from the time of day
	 */
	if (time(&tick) == -1){
		perror("time");
		return(0);
	}
	printf("seed\t%ld\n", tick);

	/*
	 * initialize the PRNG
	 */
	srand((unsigned int) tick);

	/*
	 * print the first 20 pseudo-random numbers
	 */
	for(i = 0; i < 20; i++)
		printf("%3d.\t%10d\n", i+1, rand());
	
	/* adios! */
	return(0);
}
	


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh