1 / 18

5.10 Random Number Generation

5.10 Random Number Generation. rand function Load <stdlib.h> Returns "random" number between 0 - 32767 i = rand(); Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling To get a random number between 1 and n 1 + ( rand() % n )

davenportk
Download Presentation

5.10 Random Number Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5.10 Random Number Generation • rand function • Load <stdlib.h> • Returns "random" number between 0 - 32767 • i = rand(); • Pseudorandom • Preset sequence of "random" numbers • Same sequence for every function call • Scaling • To get a random number between 1 and n • 1 + ( rand() % n ) • rand() % n returns a number between 0 and n - 1 • Add 1 to make random number between 1 and n • 1 + ( rand() % 6) • number between 1 and 6

  2. Outline fig05_07.c

  3. Outline fig05_08.c (1 of 3 )

  4. Outline fig05_08.c (2 of 3 )

  5. Outline fig05_08.c (3 of 3 )

  6. 5.12 Storage Classes • Storage class specifiers • Storage duration = Lifetime how long an object exists in memory • Scope– where object can be referenced in program • Linkage – specifies the files in which an identifier is known (more in Chapter 14) • Automatic storage • Object created and destroyed within its block • auto: default for local variables • auto double x, y; • register: tries to put variable into high-speed registers • Can only be used for automatic variables • register int counter = 1;

  7. 5.12 Storage Classes • Static storage • Variables exist for entire program execution • Default value of zero • static: local variables defined in functions. • Keep value after function ends • Only known in their own function • extern: default for global variables and functions • Known in any function

  8. Local variable: scope=lifetime Local variables are declared within the body of a function, and can only be used within that function. Static variable: scope<lifetime Another class of local variable is the static type. It is specified by the keyword static in the variable declaration. The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function. Global variable: scope =<lifetime A global variable declaration looks normal, but is located outside any of the program's functions. So it is accessible to all functions. Variable types

  9. 5.13 Scope Rules • File scope • Identifier defined outside function, known in all functions • Used for global variables, function definitions, function prototypes • Function scope • Can only be referenced inside a function body • Used only for labels (start:, case: , etc.)

  10. 5.13 Scope Rules • Block scope • Identifier declared inside a block • Block scope begins at definition, ends at right brace • Used for variables, function parameters (local variables of function) • Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block • Function prototype scope • Used for identifiers in parameter list

  11. Outline fig05_12.c (1 of 4 )

  12. Outline fig05_12.c (2 of 4 )

  13. Outline fig05_12.c (3 of 4 )

  14. Outline fig05_12.c (4 of 4 )

  15. char g = 'S’; void f1(){ char fred = 'B';printf( " %cad\n", g );g = fred; } void f2(char cg){ g=cg; } void main( void ){ char k1 = 'D‘; char k2 = ‘d’;f1();f1();g = k1;f1(); f2(k2); printf( " %cad\n", g ); }

  16. int i=33; void ff(){ printf(“\n i= %d”,i); } void main(){ int i = 99; int sum; { int i; sum = 0; for ( i=1; i<=10; ++i ) sum+= i*i; } printf( "%d\n", sum ); printf( "%d\n", i ); ff(); }

  17. int id = 1; int f1( int a){ int id = a; return ++id; } void f2(){ static int a = 4; id = ++a; printf(“\n%d - %d”,a, id); } void f3(int b){ f2(); id = f1(b); f2(); id = f1(b); } /* What is the output*/ void main(){ int c = 10; { int c; c=11; f3(c); c = id; printf(“\n%d”, c, id); } printf(“\n%d”, c, id); }

  18. int id = 1; int f1( int a){ int id = a; return ++id; } void f2(){ static int a = 4; id = ++a; printf(“\n%d - %d”,a, id); } void f3(int b){ f2(); id = f1(b); { int id = 9; id = f1(b); printf(“\n%d - %d”, id, b); } f2(); } /*ASS - What is the output*/ void main(){ int c = 10; { int c; c=11; f3(c); c = id; printf(“\n%d”, c, id); } printf(“\n%d”, c, id); }

More Related