Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.

Similar presentations


Presentation on theme: "1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible."— Presentation transcript:

1 1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible with any program! However we can generate seemingly random numbers, called pseudorandom numbers. The function rand() returns a non-negative number between 0 and RAND_MAX It is defined in stdlib.h

2 2 Random numbers /* * This program prints a list of 10 random * integers between 0 and RAND_MAX */ #include int main () { for (int i=0; i<10; i++) std::cout << rand() << std::endl; return 0; }

3 3 Random numbers The very first call to rand() uses an initial value to generate a pseudorandom number. That value is called the seed. Each subsequent call to rand() uses the result of the previous call as the starting value. rand() s 1804289383 rand()846930886 rand()1681692777 1804289383 846930886

4 4 Random numbers Starting always with the same seed will give the same sequence of numbers. In order to get a different sequence every time the program is run, we must make sure to set a different seed every time. The only thing that changes between executions is the time  use the current time as the seed.

5 5 Random numbers Set the seed for rand() with the function: To set the current time as the seed, use the time() function: Libraries needed: #include /* for rand(), srand() */ #include /* for time() */ void srand(int seed) void srand((int) time(NULL))

6 6 Random numbers Example: int main () { srand((int)time(NULL)); for (int i=0; i<10; i++) { cout << "You rolled a " << rand()%6+1 << endl; } return 0; } CAREFUL! Only use srand() once and never place it in a loop. srand() sets the seed to the current second so if it is called repeatedly within the same second (e.g. due to a loop), it will keep resetting the seed to the same value.

7 7 Random numbers When should you not set a variable seed? When you develop your project, you may want to be able to get the same sequence of numbers for debugging purposes.

8 8 Random numbers What if we want a specific range? Example: simulate dice rolling we need a random integer between 1 and 6 answer: use modulus operator rand() % 6; generates a random integer between 0 and 5 rand() % 6 + 1; generates a random integer between 1 and 6 To generate a random integer between low and high : low + rand() % (high - low + 1)

9 9 Random numbers What if we want a random floating point number within some range? Step 1: map rand() to the 0  1 range rand() / RAND_MAX is between 0 and 1 rand() / (RAND_MAX+1) is between 0 and 1, not including 1 Step 2: scale the result to desired range similarly to how we did it for integers. To generate a floating point number between low and high : low + ( rand() / RAND_MAX ) % (high - low)

10 10 Random numbers Example: Simulating a die roll: Example: Simulating a coin toss: int roll; roll = rand()%6; cout << "You rolled a " << roll << ". Roll again? (y/n)"; int toss; if (rand()%2 == 0) cout << "Heads!"; else cout << "Tails!";


Download ppt "1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible."

Similar presentations


Ads by Google