1 / 11

Random numbers in C++

Random numbers in C++. Nobody knows what’s next. Deterministic machines. That means that computers do the same predictable thing every time you give them the same instructions we WANT this – we don't want random happenings - most of the time when would you NOT want this? games

joy
Télécharger la présentation

Random numbers in C++

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 in C++ Nobody knows what’s next...

  2. Deterministic machines • That means that computers do the same predictable thing every time you give them the same instructions • we WANT this – we don't want random happenings - most of the time • when would you NOT want this? • games • simulations of reality – traffic flows, nuclear explosions, star formation • Cryptography!

  3. What is "random"? • A “real” random number is generated by a real world event like an atom decaying or not in a certain time • Hard to do that in a computer • Best we can do with a deterministic machine is make “pseudorandom” numbers • They are “good enough”

  4. “good enough”? • A “good” random number is one that is distributed evenly in its range • If you were rolling a die, you want numbers from 1 to 6 to be equally likely to show up • This is over the “long run”, not each individual trial

  5. Lots of research • Lots has been done on random numbers • Trying to get faster algorithms • With larger cycles – all algorithms will eventually start repeating but the best ones not before a few million numbers at least • Very heavy statistics and mathematics in the latest algorithms

  6. A simple one – “mid square” • Take a number to start with (the “seed”) • Square it • Take the “middle” of it – trim off some digits at front and end • That’s the random number • Repeat the process by feeding the number just generated back in as the starting number next time

  7. An example • 12345 squared = 152399025 • chop it off and get 23990 • 23990 squared = 575520100 • chop it off and get 55201 • 55201 squared = 3047150401 • chop it off and get 47150 • And so on

  8. Properties of an RNG • Give the algorithm a DIFFERENT seed to start with and what comes out? • Give the algorithm the SAME seed to start with and what comes out?

  9. Syntax • Include the cstdlib library to get the functions • Use srand(seed); to set the initial seed (seed must be an integer) • Use rand() to get the next random number • returns an integer between 0 and RAND_MAX • Call srand ONCE per program, call rand many times

  10. Seeds to start with • srand(23); will always give you the same sequence of random numbers – good when testing! • Asking the user for a number and then using it as the seed - works but is a bit aggravating to the user • Using the time function is most flexible (see example on web page)

  11. rand() and RAND_MAX • rand() returns a integer between 0 and RAND_MAX (a constant defined in cstdlib) • What if you need an integer number between 1 and 6? rand() % 6 + 1 • What if you need a number between 0 and 1 (i.e, a fraction) ? rand() / RAND_MAX (but watch your TYPE here!)

More Related