Programming Codes with Output - Introduction to Computing | ECS 102, Study notes of Computer Science

Material Type: Notes; Professor: Baruch; Class: Introduction to Computing; Subject: Engineering & Computer Science; University: Syracuse University; Term: Fall 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-yzd
koofers-user-yzd 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/* 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.
pf3
pf4

Partial preview of the text

Download Programming Codes with Output - Introduction to Computing | ECS 102 and more Study notes Computer Science in PDF only on Docsity!

/* 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: