250 likes | 274 Views
CSIS 113A. Lecture 5 Random Numbers, while, do-while. Random Numbers. rand() – Returns a pseudo-random integral number in the range 0 to RAND_MAX number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called.
E N D
CSIS 113A Lecture 5 Random Numbers, while, do-while
Random Numbers • rand() – Returns a pseudo-random integral number in the range 0 to RAND_MAX • number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. • RAND_MAX is a constant defined in <cstdlib>. • Its default value may vary between implementations but it is guaranteed to be at least 32767.
Scaling • Use the mod operator % • x = rand() % 100; • Generates a random number between 0 & 99 • More rand() • rand() % 100 + 1 is in the range 1 to 100 • Typical use with variables • Val = (rand() % max) + min; • Returns random number between min and max
Seeding • Can interject more randomness in algorithm by seeding it • srand(value); • Where do you get value? • Best to seed from time of day clock • Must include <ctime> • Use time(0) so • srand(time(0)) • Only call srand() one time • Do not include in a loop!
Example #include <iostream>#include <ctime> using namespace std;int main(){ srand(time(0)); // See from time of day clock for(int i = 0; i < 10; i++) { cout << (rand() % 100) + 1 << endl; } }
The while Loop Syntax • Parts of a while • Keyword while, a boolean test, loop body
Counted while Loops • To use the while statement to build a counted loop, you must do three things: • 1.Createa counter variable, and initialize it before the loop is encountered. • 2. Test the counter variable inside the while loop's boolean condition expression • 3. Updateyour counterat the end of the loop body. This takes the place of the for loop's update expression.
A Counted while Skeleton • int counter = 0;while (counter < 10 ){ // Loop body statementscounter++;}
Common while Loop Errors I • Several problems common to counted while loops • Using an "expired" counter • Define and initialize counter in for initializer • Looks empty if you forget initialization • Allows you to use the same variable name in each loop • The while requires counter initialization before loop • You must remember to initialize your counter just before you enter the loop
Common while Loop Errors II • Endless Loops • Not unique to while loops • Change exponential for loop bounds to 100000 • Counted while loops are especially susceptible • Counter update is often far removed from test • Makes it easy to forget the update • Solution? • Use loop-building strategy that starts with bounds • Get mechanics working before attacking the goal
Common while Loop Errors III • The phantom semicolon • If you put a semicolon after the test condition of a while loop, its body becomes the null statement • int counter = 0;while (counter < 10 );{// This is unreachable counter++;}
Indefinite Loops • How many times will this loop execute? int someNumber = rand() % 1000;while ( someNumber != 500 ){ someNumber = rand() % 1000; cout << someNumber << endl; } • You can’t tell. That’s why it’s an indefinite loop • This is where the while loop really shines
Indefinite • One way Indefinite works • integer variable someNumber is assigned a value between 0 and 999, using the rand() function • Variable someNumber is compared to 500 in while test • If it is not equal to 500 then the loop body is entered • In the loop body, the number of repetitions is incremented and displayed • A new value for someNumber is then generated • This kind of indefinite loop is called a SentinelLoop • A specific value [500] is searched for
Sentinel Ranges I • Sentinel doesn't have to be a single value • It may include a range of values • Here's a problem that requires a sentinel range "Generate random integer numbers until a negative number is generated. Display the sum, count, and average of the numbers entered"
Sentinel Ranges II • Questions you should ask • 1. What is the loop's bounds? • 2. What are the necessary preconditions? • 3. What actions are required to advance the loop? • 4. What is the loop's goal? • 5. What are the goal preconditions? • 6. What actions are necessary in the body? • 7. What postconditions are necessary for the goal?
Sentinel Ranges III • 1. What is the loop's bounds? • Answer: A negative number was generated • The C++ loop condition should look like this: while (num >= 0) • 2. What are the necessary preconditions? • Look at the test condition; num must have a value • int num = rand();
Sentinel Ranges IV • 3. What actions advance the loop? • Must change something in the test expression • Should generate another random number • At this point, the "mechanics" are finished • You can test the loop to see that it works • 4. What is the loop's goal? • Display the count, sum and average of positive integers generated
Sentinel Ranges V • What are the goal preconditions? • Must have variables for the count and sum • Sum should be a double because of overflow • count and sum must both be initialized int count = 0; double sum = 0.0; • 6. What actions are necessary to advance goal? • Add num to sum, incr count, before next num count++; sum += num;
Sentinel Ranges VI • 7. What post-condition actions are required? • Must check for a count of zero. • Means no numbers were entered • Can't compute the average if there are no numbers • If count is > 0 then • Compute and display the sum and the average
Primed and Inline Tests I • So far, all our indefinite loops have been primed • Initialize the test condition before the loop • Usually this involves reading a value • This is called priming the loop • Test the value in the loop condition • Process the value in the body of the loop • Initialize the test condition at bottom of loop body • Preparing for the next repetition of the loop • Means "preparation" statements appear in two places
Intentional and Necessary Bounds • Suppose you search a String for the letter "F" • A sentinel bounds is the obvious choice while (s[i] != 'F') i++; • But what if the letter 'F' is not in the String s ? • You need to supply an additionalbounds to use in case the value you are looking for is not found • This additional bounds is called a necessary bounds while (i < s.length && s[i] != 'F') i++;
Two Logical Problems • The impossiblecondition if (age > 65 && age < 21) … • Solution? • Change order if you are trying to bound value • Change AND to OR • The unavoidablecondition if (age > 21 || age < 65) • Solution? Change OR to AND
Bottom-Testing • One more loop in addition to for and while • The do-while loop is an exit-condition loop • That means the test is located after the body do{ Statements;} while ( boolean condition ); • Notice position of semicolon • Generally not the loop of choice. [See next page]
C++ Jumps: break and continue • Two jump statements from inside a loop • To restart the loop: use continue • For while and do-while this jumps to the test • With for it jumps to the updateexpression • To exit the loop: use break for (int i = 0; i < 7; i++){ if ( i % 2 == 0) continue; if ( i == 5 ) break; cout<< i << endl;}