Simulation-4

# Simulation-4 - The rand Function Random Numbers In many...

This preview shows pages 1–2. Sign up to view the full content.

1 Random Numbers In many applications, want to be able to generate random numbers, permutations, etc. Since computers are deterministic, true randomness does not exist Settle for pseudo-randomness : sequence of numbers that looks random, but is deterministically generated Most random number generators use a “linear congruential generator” (LCG): o Start with a seed number X 0 o Next “random” number given by: X n+1 = ( a X n + c ) mod m o Effectiveness is very sensitive to choice of a , c , and m o Note: the sequence of random numbers must eventually cycle The rand() Function In C/C++, there exists a function for generating pseudo-random numbers: int rand(void) Part of <stdlib.h> library Returns integer between 0 and RAND_MAX , inclusive o Values supposed to be “uniformly” distributed in range o RAND_MAX guaranteed to be at least 32767 (often larger) In most implementations, uses LCG Seeded using: void srand(unsigned int seed) o Often set to current system time: srand(time(NULL)); For our purposes, we accept approximation: (rand()/((double)RAND_MAX + 1)) ~ Uni[0, 1) Shuffling Deck of Cards Want to generate a random permutation of set E.g., completely shuffle a deck of cards Want all permutations to be equally likely void shuffle(int arr[], int n) { for(int i = n - 1; i > 0; i--) { double u = uniformRand(0, 1); // u in [0, 1) // pick one of "remaining" i positions int pos = (int)((i + 1) * u); swap(arr[i], arr[pos]); } } Bad, But Common, Shuffle Common mistake in creating random permutation void badShuffle(int arr[], int n) { for(int i = 0; i < n; i++) { double u = uniformRand(0, 1); // u in [0, 1) // pick any position int pos = (int)(n * u); swap(arr[i], arr[pos]); } } Has n n execution paths, but only n! permutations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.
• Spring '09
• Probability distribution, Randomness, Cumulative distribution function, Discrete probability distribution, Inverse Transform

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern