1 / 10

Random numbers

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.

azura
Download Presentation

Random numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 functionrand()returns a non-negative number between 0 and RAND_MAX • It is defined in stdlib.h

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

  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. s rand() 1804289383 1804289383 rand() 846930886 846930886 rand() 1681692777

  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. 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<cstdlib> /* for rand(), srand() */#include<ctime> /* for time() */ void srand(int seed) void srand((int) time(NULL))

  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. 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. 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. 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 1rand() / (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. 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!";

More Related