#include #include #include int main() { int seed; cout << "Enter seed: "; cin >> seed; // if user enters -1, seed with time of day if (seed == -1) seed = time(0); srand(seed); // generate a random number between 0 and 99 for (int i=0;i<10;++i) cout << rand()%100 << endl; // generate a random number between 0 and 1 int count = 0; for (int i=0;i<1000;++i) count += rand()%2; cout << "the count is " << count << endl; // generate a biased coin flip with 75% head, 25% tail for (int i=0;i<10;++i) { if (rand()%4 < 3) cout << "head\n"; else cout << "tail\n"; } cout << "----\n"; // generate a biased coin flip with 42% head, 58% tail int head_ct = 0, tail_ct = 0; for (int i=0;i<100;++i) { if (rand()%100 < 42) ++head_ct; else ++tail_ct; } cout << head_ct << " heads, " << tail_ct << " tails\n"; // generate floating random numbers 0 to slightly less than 1 for (int i=0;i<100;++i) cout << rand()/32768. << endl; // pick randomly from A, B, or C with probabilities .6, .3 and .1 // The range 0-.6 will be for A, .6-.9 for B, and .9-1.0 for C. double r; for (int i=0;i<100;++i) { r = rand()/32768.; if (r < .6) cout << "A" << endl; else if (r < .9) cout << "B" << endl; else cout << "C" << endl; } // Note: for the elevator assignment, you will have 9 (0-8) floors // and you will have to generate a floor choice from a distribution // over these 9 floors. You can do it with a bunch of if/else if/else // statements, but there's a more efficient way with a loop and having // the probabilities in an array. Think about it. One clue: // the first if statement can be written as "if (r < 0.+.6)" // and the last else statement can be written as "else if (r < .9+.1)" }