rand.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
	int i;
	time_t tick;


	printf("First, rand() without seeding ...\n");
	for(i = 0; i < 5; i++)
		printf("%10d ", rand());
	putchar('\n');

	tick = time(NULL);
	printf("Next, rand() with a seed (%ld)\n", tick);
	(void) srand((unsigned int) tick);
	for(i = 0; i < 5; i++)
		printf("%10d ", rand());
	putchar('\n');

	printf("First, random() without seeding ...\n");
	for(i = 0; i < 5; i++)
		printf("%10ld ", random());
	putchar('\n');

	printf("Next, random() with a seed (%ld)\n", tick);
	(void) srandom((unsigned int) tick);
	for(i = 0; i < 5; i++)
		printf("%10ld ", random());
	putchar('\n');

	printf("Now pause for a second ...\n");
	(void) sleep(1);

	tick = time(NULL);
	printf("Next, random() with a different seed (%ld)\n", tick);
	(void) srandom((unsigned int) tick);
	for(i = 0; i < 5; i++)
		printf("%10ld ", random());
	putchar('\n');

	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