Download presentation
Presentation is loading. Please wait.
1
CSC 107 - Programming for Science Lecture 22: Random Numbers & Macros
2
Question of the Day On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door, the host, opens another door which has a goat. He then asks, "Do you change your selection?” Should you?
3
Proving This Solution After 1 experiments ---------------------- Sticking it out: 100.00% correct Switching doors: 0.00% correct After 1000 experiments ---------------------- Sticking it out: 34.10% correct Switching doors: 68.20% correct After 1000000 experiments ---------------------- Sticking it out: 33.40% correct Switching doors: 66.72% correct
4
Pseudo-random Numbers Computers are stupid & only follow orders Cannot be random or do unexpected things Algorithm generates sequence of integers No correlation between numbers in sequence (One hopes) Sequence should be very long Numbers completely predictable Just have to know the algorithm Or wait long enough to see patterns repeat
5
Pseudo-random Numbers
6
Seeding Pseudo-random number generators rely upon a seed Value used to start sequence of random numbers A seed always yields identical sequence of random numbers Useful for testing & debugging code Bad when comparing runs Usually seed with current time: srand(times(NULL));
7
rand() Pre-defined function Prototype in stdlib.h Generates integer between 0 & RAND_MAX RAND_MAX also from stdlib.h To generate a random number… … between 0 - 5: (rand() % 6) … between 5 - 10: (rand() % 6) + 5 … between -5 - 0: (rand() % 6) - 5 … between -25 - 25: (rand() % 51) - 25
8
Results For Staying correct = 0 for (i = 0; i < experiments; i++) { guess = rand() % 3 solution = rand() % 3 if (guess == solution) { correct += 1; } pct = correct /((double)experiments) * 100;
9
Results For Switching correct = 0 for (i = 0; i < experiments; i++) { guess = rand() % 3 solution = rand() % 3 if (guess != solution) { /* Other incorrect solution revealed, so switch to correct solution */ correct += 1; } pct = correct /((double)experiments) * 100;
10
Warning!!!! Many systems have bad rand() versions Generates random numbers, … … but randomness limited to largest digits So, remainders will not be that random Recommend that they not be used Instead round random decimal numbers Decimal numbers use those largest digits
11
Random Decimal Numbers No function generates random decimals Just use rand() / (RAND_MAX + 0.0) To generate random decimal number… … between 0 - 1: rand()/(RAND_MAX+0.0) … between 0 - 5: rand()/(RAND_MAX+0.0)*5 … between -5 - 0: rand()/(RAND_MAX+0.0)*-5 … between -25 - 25: (rand()/(RAND_MAX+0.0)*50)-25
12
Symbolic Constants Pre-processor replaces name with value Compiler only sees the constant value Programmer only sees the name Makes code far easier to read, write, debug Names traditionally in all CAPITAL letters
13
What You Write And Work With #define PI 3.1415962 #define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = PI * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE);
14
#define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = 3.1415962 * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE); #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = 3.1415962 * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE); #define DUMB_EXAMPLE 10.5 area = 3.1415962 * (r * r); printf(“%lf”, 10.5); printf(“%lf”, DUMB_EXAMPLE); area = 3.1415962 * (r * r); printf(“%lf”, 10.5); printf(“%lf”, 10.5); What The Compiler Sees #define PI 3.1415962 #define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = 3.1415962 * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE);
15
Macros Powerful cousins of symbolic constants Shares similar traits with their relatives Begin with #define Must be only code on line (and fit one 1 line) Name usually in all CAPITAL Text replaced before compilation But can use parameters Value should be placed in parenthesis
16
Creating a Macro Macro can be very simple #define RAND_DEC (rand()/(RAND_MAX+0.0)) Macros can also take parameters #define SQUARED(x) (x * x) #define VELOCITY(init,a,t) (init+(a*t)) #define RAND_X(x) (rand() % x) #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y) ? x : y) Parameters replaced during compilation This will cause problems in code, be careful!
17
What You Write And Work With #define RAND_DEC (rand()/(RAND_MAX+0.0)) #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y)? x : y) int i = RAND_RNG(4, 7); double rnd = RAND_DEC; int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++));
18
What the Compiler Sees #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y)? x : y) int i = RAND_RNG(4, 7); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++)); #define MAX(x,y) ((x > y)? x : y) int i = ((rand()%(7-4))+4); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++)); int i = ((rand()%(7-4))+4); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, (i > j++)? i : j++);
19
Your Turn Try doing following problems on your own
20
For Next Lecture Continue programming assignment #2 Now due next Wednesday – 11/01/06 Finish week #9 weekly assignment
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.