上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics

上海交通大学交大密西根 联合学院·一 ◆ 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

上海交通大学交大密西根 联合学院·一 ◆ 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);
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Solution for Practice Final 1.pdf