上海交通大学交大密西根 ·联合学院一 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University All Functions Defined in RandomT RandomT (){seed =(int)time(NULL);} Constructor without parameter RandomT(const int initSeed){ Constructor with initial seed seed initSeed;} provided int nextInteger(int lower,int upper); range in [lower,upper] int nextInteger (int limit); range in [0,limit-1] double nextDouble range in [lower,upper) (double lower,double upper); double nextDouble () range in [0,1) bool nextBoolean (double p); Returns a random boolean,which is true with probability p60,11
All Functions Defined in All Functions Defined in RandomT RandomT RandomT () {seed = (int) time(NULL);} RandomT (const int initSeed) { seed = initSeed;} int nextInteger (int lower, int upper); int nextInteger (int limit); double nextDouble (double lower, double upper); double nextDouble (); bool nextBoolean (double p); Constructor without parameter Constructor with initial seed provided range in [lower, upper] range in [0, limit-1] range in [lower, upper) range in [0, 1) Returns a random boolean, which is true with probability p Є[0, 1]
Implementing Methods After you define a class,you need to provide the implementation for each method in the class.Because two different classes may each have a member function with the same name,you need to specify the class name when defining it.For instance,the nextInteger (method of the RandomT class might be defined like this: int RandomT:nextInteger (int high) { double d; d =(double)next()/((double)m+1); return (int)(d high);
Implementing Methods Implementing Methods • After you define a class, you need to provide the implementation for each method in the class. Because two different classes may each have a member function with the same name, you need to specify the class name when defining it. For instance, the nextInteger () method of the RandomT class might be defined like this: int RandomT::nextInteger (int high) { double d; d = (double) next() / ((double) m + 1); return (int) (d * high); }
上海交通大学交大密西根 B 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Instantiate Using Objects Now that we've defined the RandomT class and implemented its methods,we can now create instances of it. ● Create a instance of class is to define an object,which is as easy as to define a variable like RandomT randInt;
Instantiate Using Objects Instantiate Using Objects • Now that we’ve defined the RandomT class and implemented its methods, we can now create instances of it. • Create a instance of class is to define an object, which is as easy as to define a variable like RandomT randInt;
上海交通大学交大密西根 联合学院·一 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Write a Test Program Now,let's write a test program to see how does the random class work. We will write a program to simulate rolling a die.The die has 6 faces which is labeled with 1-6 respectively.We want write a program to prove that one you rolling the die enough times,the chance you get from each side is the same-1/6
Write a Test Program Write a Test Program • Now, let’s write a test program to see how does the random class work. • We will write a program to simulate rolling a die. The die has 6 faces which is labeled with 1-6 respectively. We want write a program to prove that one you rolling the die enough times, the chance you get from each side is the same-1/6
Test Your Class void RollDie (void) int main (void) RandomT dieFaceNumj int intFace1 0,intFace2 =0,intFace3 0,... RollDie O; for (inti =0;i<=RAND_MAX;i++) retur 0; int faceNum dieFaceNum.nextlnteger (1,6); if (faceNum ==1) C:\Windows\system32\cmd.exe intFace1++; The probability of face number 1 is:16.684 else if (faceNum =2) The probability of face number 2 is:16.7847 The probability of face number 3 is:16.4917 The probability of face number 4 is:16.7755 intFace2++; The probability of face number 5 is:16.4276 The probability of face number 6 is:16.8365 岁 console.printLine (The probability of face number =1 is:" (double)intFace1/(RAND_MAX+1)*100,endl); console.printLine (The probability of face number 2 is:", (double)intFace2/(RAND_MAX+1)*100,endl); 想
Test Your Class Test Your Class