


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Baruch; Class: Introduction to Computing; Subject: Engineering & Computer Science; University: Syracuse University; Term: Fall 2009;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



/* userand.c / #include<stdio.h> / printf / #include<stdlib.h> /rand, srand, RAND_MAX/ #include<time.h> / time / int main(void) { int i; / print some random numbers */ printf("Some random numbers\n"); for (i= 1 ; i<= 5 ; i++) printf("%10d", rand()); printf("\n"); printf("Range is 0 to %d.\n\n",RAND_MAX); Some random numbers 16838 5758 10113 17515 31051 Range is 0 to 32767.
/* How to create a good range. Suppose we want numbers in the range 2 - 5. 2 3 4 5 That is 4 consecutive numbers. Step 1: take mod 4. / printf("4 consecutive numbers.\n"); for (i= 1 ; i<= 20 ; i++) printf("%3d", rand()% 4 ); printf("\n\n"); / But you want the lowest one to be 2, so add 2. */ printf("random numbers form 2 to 5 \n"); for (i= 1 ; i<= 20 ; i++) printf("%3d", rand()% 4 + 2 ); printf("\n"); 4 consecutive numbers. 3 2 3 0 2 1 3 0 0 1 3 1 3 1 2 2 2 3 3 3 random numbers form 2 to 5
/* The computer can seed with the time / printf("\n\nThe time is now %d\n", time( 0 )); printf("Using the time as a seed: \n"); srand(time( 0 )); /Typically do this once at the beginning of your program */ for(i= 1 ; i<= 20 ; i++) printf("%3d",rand()% 100 ); return( 0 ); } The time is now 1110321446 Using the time as a seed: