Discussion 10-25

Random numbers

Today we did examples showing uses of random numbers. The syntax we know for getting random numbers is:

This "seeds" the random number generator.
Without this line, your program will generate the same random numbers each time.
You only need to run this line once, at the start of main.

srand( time(0) );

int myRandomNumber = rand();
 // myRandomNumber now holds a random INTEGER from 0 to RAND_MAX. 

int a = 6; int b = 25;
int randomFromAtoB = (myRandomNumber % (b-a+1)) + a;
// randomFromAtoB now holds a random integer from a to b, inclusive.

double c = 3.6; double d = 4.1;
double randomFromCtoD = ((double)myRandomNumber / RAND_MAX) * (d-c) + c;
 // randomFromCtoD now holds a random DECIMAL NUMBER in the interval [c,d]. 


Google searches

Here are some Google searches you could try if you're having trouble with today's material. I'm deliberately trying to choose search terms that you could have thought of yourself!

Extra stuff to try for fun (not required)

The "Monte Carlo" method can let you compute (or at least approximate) lots of complicated probabilities. Here are two of the first things I tried MC on back when I first learned this stuff:

  1. In the casino game craps, the two most basic bets are "pass" and "don't pass". Which one has a bigger house edge?
  2. Let's say you play the board game Monopoly forever and never run out of money. What are the top 3 most common squares you'll end up on? How does that change if instead of using regular dice you try using one 6-sided die and one 4-sided, or some other weird combination? Here is a resource that explains all the Monopoly rules that matter for this puzzle.