上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Random Numbers Introduction to ANSI C++
Vg101: Introduction to Vg101: Introduction to Computer and Programming Computer and Programming Random Numbers Introduction to ANSI C++
Random Numbers Deterministic programs are useful,but we also want to model non-deterministic events. Computers are deterministic,for any given set of inputs we can predict the output of the program. Need some mechanism to simulate a random process, such as flipping a coin or tossing a die. Pseudo-random numbers are generated by a deterministic process,but 1)Behave like random numbers statistically 2)Are hard to predict without knowledge of the process
上海交通大学交大密西根 联合学院·一 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University class RandomT class RandomT int seed; int next(); public: RandomT ({seed =(int)time(NULL);} RandomT (const int initSeed){seed initSeed;) int nextInteger (int lower,int upper); /range in [lower,upper] int nextInteger (int limit); ∥range in0,limit--l] double nextDouble(double lower,double upper); /range in [lower,upper) double nextDouble () ∥range in0,1) bool nextBoolean (double p); //Returns a random boolean,which ∥is true with probability p,where0≤p≤l
class RandomT RandomT class RandomT { int seed; …. int next(); public: RandomT () {seed = (int) time(NULL);} RandomT (const int initSeed) {seed = initSeed;} int nextInteger (int lower, int upper); // range in [lower, upper] int nextInteger (int limit); // range in [0, limit-1] double nextDouble (double lower, double upper); // range in [lower, upper) double nextDouble (); // range in [0, 1) bool nextBoolean (double p); // Returns a random boolean, which // is true with probability p, where 0 ≤p ≤1. }
上海交通大学交大密西根 ·联合学院一 UM-SJTU Joint Institute 1Ua日 181 University of Michigan Shanghal Jiao Tong University seed AND next ( That number becomes next ( 12384 the new seed, Seed 12384
seed AND next () next () • That number becomes the new seed
上海交通大学交大密西根 B 联合学院·一 ◆ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Pseudo-Random How does next ( work?It starts with a seed (0 in next ( ◆12384 this example)and applies a certain formula to Seed generate a new 0 pseudo-random number
Pseudo-Random • How does next () work? It starts with a seed (0 in this example) and applies a certain formula to generate a new pseudo-random number