CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved...
-
Upload
letitia-blair -
Category
Documents
-
view
214 -
download
0
description
Transcript of CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved...
![Page 1: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/1.jpg)
CSci 162
Lecture 7
Martin van Bommel
![Page 2: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/2.jpg)
Random Numbers
• Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values
• Some applications (e.g. games) require unpredictable behavior - nondeterministic
• One method for nondeterminism die toss where one of several outcomes possible
![Page 3: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/3.jpg)
Random Numbers
• Number is random if there is no way to determine in advance what value it will have among a set of equally probable possibilites
• E.g. on a standard unloaded die– random number between 1 and 6 equally possible
• Cannot generate number randomly on computer– uses deterministic behavior
![Page 4: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/4.jpg)
Pseudo-random Numbers
• Use a deterministic procedure to generate “random” numbers
• Numbers must appear random– behave like random numbers statistically– be sufficiently difficult to predict in advance
• Random numbers generated by an algorithmic process by a computer are called pseudo-random numbers
![Page 5: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/5.jpg)
Pseudo-random number in C++
• <cstdlib> includes function rand and constant RAND_MAX
• prototype int rand();• generates pseudo-random number between 0 and RAND_MAX, inclusive
• Value of RAND_MAX depends on system• Ours uses 32 767 but your program should not
rely on a given value but on the symbolic constant
![Page 6: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/6.jpg)
Using “rand” to flip a coin
• Want to use the result of rand that is a number between 0 and RAND_MAX to generate Heads or Tails
• Useif (rand() <= RAND_MAX / 2) cout << ”Heads”;else cout << ”Tails”;
![Page 7: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/7.jpg)
Wrong Method to flip a coin
• Why not use:if (rand() % 2 == 0) cout << ”Heads”;etc.
• No guarantee of distribution of even and odd numbers, only even distribution along the range 0 to RAND_MAX
![Page 8: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/8.jpg)
Using “rand” to Roll a Dieint RollDie(void){ if (rand() < RAND_MAX / 6) return 1;if (rand() < RAND_MAX*2/6) return 2;if (rand() < RAND_MAX*3/6) return 3;if (rand() < RAND_MAX*4/6) return 4;if (rand() < RAND_MAX*5/6) return 5;return 6;
}• Unfortunately uses new value of “rand” in each
“if”, thus losing original.
![Page 9: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/9.jpg)
Using “rand” to Roll a Die (2)int RollDie(){ int r = rand();if (r < RAND_MAX / 6) return 1;if (r < RAND_MAX*2/6) return 2;if (r < RAND_MAX*3/6) return 3;if (r < RAND_MAX*4/6) return 4;if (r < RAND_MAX*5/6) return 5;return 6;
}• Unfortunately RAND_MAX*2/6 gives strange value due to
integer arithmetic• Better to use RAND_MAX/6.0*2.0
![Page 10: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/10.jpg)
Generalize the Problem
• Want a function to choose random number between two values, inclusive int RandomInteger(int low, int high);
• Four-step process1. Normalize rand to number d where 0 <= d < 12. Scale d by multiplying by size of desired range3. Truncate number back to an integer4. Translate integer so range begins at lower bound
![Page 11: CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.](https://reader036.fdocuments.us/reader036/viewer/2022082908/5a4d1b307f8b9ab05999afc9/html5/thumbnails/11.jpg)
RandomInteger
int RandomInteger(int low, int high){ int k; double d;
d = rand() / ((double) RAND_MAX + 1.0); k = (int) (d * (high - low + 1)); return (low + k);}