上海交通大学交大密西根 联合学院·一 ◆ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Pseudo-Random The new seed is used to produce the next random number, next ( 17 which will become the next seed,and so on. The numbers that the function produces Seed appear random to the 12384 user
Pseudo-Random • The new seed is used to produce the next random number, which will become the next seed, and so on. • The numbers that the function produces appear random to the user. next ()
上海交通大学交大密西根 ·联合学院一 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Initialization of a seed Another question:how does seed get initialized?There are two ways.One is to provide a default value as part of the declaration. class RandomT { int seed; int next(); public: /Constructor Functions: RandomT (){seed =(int)time(NULL);} RandomT (const int initSeed){seed initSeed;)
Initialization of a seed Initialization of a seed • Another question: how does seed get initialized? There are two ways. One is to provide a default value as part of the declaration. class RandomT { int seed; …. int next(); public: // Constructor Functions: RandomT () {seed = (int) time(NULL);} RandomT (const int initSeed) {seed = initSeed;}
上海交通大学交大密西根 联合学院·一 ◆ 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Random number ranges Next (is very general function.Sometimes,we need a random distribution in certain range. Take coin flipping as an example: - if the random range defined by the system is RAND MAX then we could take the first half as HEAD and the last half as TAIL 0 RAND MAX HEAD TAIL Is it OK if we take (next ()%2)for HEAD?
Random Number Ranges Random Number Ranges • Next () is very general function. Sometimes, we need a random distribution in certain range. • Take coin flipping as an example: – if the random range defined by the system is RAND_MAX then – we could take the first half as HEAD and the last half as TAIL – Is it OK if we take (next () % 2) for HEAD? 0 RAND_MAX HEAD TAIL
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Random number ranges Simulating a die roll? RAND_MAX 1T2T3T456 If you don't think this problem carefully, there are several things can go wrong. Let's describe the problem like this If you continuously rolling the dies,the probability you got for each fact are the same
Random Number Ranges Random Number Ranges • Simulating a die roll? • If you don’t think this problem carefully, there are several things can go wrong. • Let’s describe the problem like this – If you continuously rolling the dies, the probability you got for each fact are the same 0 RAND_MAX 1 2 3 4 56
int RollDie (void) int intDieFaceNumber; if (next (RAND MAX 6) { intDieFaceNumber 1; 》 else if (next (RAND MAX 2/ 6) { intDieFaceNumber 2; 》 else if (next ( RAND_MAX¥3/ 6) { intDieFaceNumber 3¥ } else if next (RAND MAX x/6) { intDieFaceNumber 4; 》 else if next (RAND MAX x 5/6) { intDieFaceNumber 5; 》 else { intDieFaceNumber 6; return (intDieFaceNumber);