Lec13RandomnessSimulation_6per.pdf

Lec13RandomnessSimulation_6per.pdf - EECS 402 Random...

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

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 13 Savitch N/A Randomness In Programming Simulations EECS 402 EECS 402 Andrew M Morgan 2 Random Numbers Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers that come up on the roulette wheel are random Writing a program such that the numbers that come up the same every time doesn't make for a very interesting roulette game Suppose you are writing a simulation of a customer walking through the airport Statistics can be obtained about this type of simulation "A customer enters the airport on average once every 4 seconds", or "A customer spends, on average 8 minutes in a bookstore" Your simulation might result in generating a new customer after a random time interval (given a uniform distribution around 4 seconds) EECS 402 EECS 402 Andrew M Morgan 3 Computers and Random Numbers Computers can not generate truly "random" numbers Instead, pseudo-random numbers are used Pseudo-random numbers are generated using a mathematical formula Consider this formula: next = int((prev / 50) + prev * 17) With a start value of 4, the numbers generated would be: 4, 68, 1157, 19692, 335157, ... This list is not very "random" looking - it is always increasing at a fast rate EECS 402 EECS 402 Andrew M Morgan 4 A Better Pseudo-Random Formula? The Midsquare method is another method of generating random numbers. To generate numbers of X digits, square the previous number and use the middle X digits 384 2 = 01 474 56 => 474 474 2 = 02 246 76 => 246 246 2 = 00 605 16 => 605 605 2 = 03 660 25 => 660 660 2 = 04 356 00 => 356 356 2 = 01 267 36 => 267 267 2 = 00 712 89 => 712 712 2 = 05 069 44 => 069 069 2 = 00 047 61 => 047 047 2 = 00 022 09 => 022 022 2 = 00 004 84 => 004 004 2 = 00 000 16 => 000 000 2 = 00 000 00 => 000 000 2 = 00 000 00 => 000 000 2 = 00 000 00 => 000 Start with the number 384 (for three digit pseudo-random #s) This generator looks better at first. It isn't constantly increasing like the last one. However, it is repeating . Once it gets to the number 0, the number 0 is repeated over and over, which makes for a poor generator. EECS 402 EECS 402 Andrew M Morgan 5 Another Generator The midsquare method on the previous page resulted in the value 0 being repeated after it is generated Some generators actually result in repeating patterns, which sometimes can be difficult to find The Linear Congruential Method uses the formula xn+1 = (a xn + c) % m If a==2, c==3, m==10, and x0 == 0, the sequence is: x 1 = (2 X 0 + 3) mod 10 = 3 x 2 = (2 X 3 + 3) mod 10 = 9 x 3 = (2 X 9 + 3) mod 10 = 1 x 4 = (2 X 1 + 3) mod 10 = 5 x 5 = (2 X 5 + 3) mod 10 = 3 x 6 = (2 X 3 + 3) mod 10 = 9 x 7 = (2 X 9 + 3) mod 10 = 1 x 8 = (2 X 1 + 3) mod 10 = 5 x 9 = (2 X 5 + 3) mod 10 = 3 Repeating Pattern EECS 402 EECS 402 Andrew M Morgan 6 An Advantage... The same sequence of pseudo-random numbers can be generated This is good, because it is difficult to debug programs that use truly random numbers By providing the same start-value (called a seed), the same sequence results, which means you can generate the same results time and time again When you are through debugging, you can use a different seed each time.
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern