# 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].
```

## Examples

• guessingGame.cpp implements a well-known "guess the number" game, using randomization to make sure the number truly is unknown each time.
• probability.cpp shows a real-life useful application of random numbers. When you want to know the probability of (EVENT) happening but you don't feel like working out the formula, you can always just tell your computer to run a ton of simulations and record what fraction of the time (EVENT) happened. If you did enough trials, that can be a very accurate method to approximate the true probability. People call this a "Monte Carlo" method for estimating probabilities. This example shows the technique by estimating the probability that exactly 5 coins will come up heads if we toss 7 coins total. The exact answer is given in a comment at the top of the file. Try running the program a few times with various numbers of trials and see how accurate your results are!
• We talked briefly about how you could set up the integral estimation for your homework assignment. Let's say we want to estimate the integral from a to b of f(x). I think you should proceed roughly like:
1. Choose a random real number x from the interval [a,b].
2. Estimate the area under the curve from a to b by assuming it's just a rectangle, with width (b-a) and height f(x). This gives you an approximate value for the integral, but the approximation won't typically be very accurate.
3. Run steps 1 and 2 a bajillion times. (OK, however many times the user specified.) Each runthrough gives you an approximate value for the integral. Your final computed integral should be the AVERAGE of each of those approximations. So if the user asked for 1000 iterations, then you'll do steps 1+2 1000 times and your answer will be the average of those 1000 results.

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!

• c++ rand and c++ srand
• monte carlo method wikipedia - Cool to see all the crazy probabilities people estimate this way! In math class, probabilities are always specific numbers you can compute, but in real life it's very common to accept that a simulation / approximate is the best we can hope for.

## 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.