Chapter 14. Random Numbers

Similar documents
Random Numbers. Chapter 14

Chapter 5. Dialog Boxes

Chapter 8. Nested Selections

Chapter 6. Selection

Dialog Boxes. Chapter 5

Loops. Chapter 10. The WHILE statement

Chapter 23. Inheritance and Polymorphism

Discrete-Event Simulation:

Linked Lists. Chapter 21

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Chapter 15. One-Dimensional Arrays

Nested Selections. Chapter 8

Errata for C++ and Algorithmic Thinking for the Complete Beginner

Discrete-Event Simulation:

Section 3.2: Multi Stream Lehmer RNGs

Binary Trees. Chapter 22

Add Subtract Multiply Divide

Proper Procedures. Chapter 12

Variable initialization and assignment

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Chapter 18. Two-Dimensional Arrays

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

REVIEW. The C++ Programming Language. CS 151 Review #2

The State Design Pattern

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

ISA 563 : Fundamentals of Systems Programming

CS1100 Introduction to Programming

Computer Internals ECE2036. Lecture 2. ECE2036 Computer Internals Spring / 12

Chapter 2 Random Number Generators

Lecture 8: Addition, Multiplication & Division

Chapter 4 Section 2 Operations on Decimals

Index. Numerics 0X String sentinel 65, Invariant violation Precondition violation 120

Operators. Lecture 3 COP 3014 Spring January 16, 2018

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Credit Cards. Validating Credit Cards. Answers

Selection. Chapter 6. Figure 6.1 The relational operators. = Equal to. < Less than. <= Less than or equal to. > Greater than

Last Time: Rolling a Weighted Die

Function Call Stack and Activation Records

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

LAB 5 Arithmetic Operations Simple Calculator

Session 04 - Object-Oriented Programming 1 Self-Assessment

One-Dimensional Arrays

Consider the actions taken on positive integers when considering decimal values shown in Table 1 where the division discards the remainder.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

C Functions. 5.2 Program Modules in C

hp calculators HP 9g Probability Random Numbers Random Numbers Simulation Practice Using Random Numbers for Simulations

The. Binary. Number System

>>> Assignment #4 for Simulation (CAP 4800) <<<

Modules and Interfaces

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Octal & Hexadecimal Number Systems. Digital Electronics

Names and Functions. Chapter 2

Chapter 3 - Functions

Two-Dimensional Arrays

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

GCSE Computer Science Booster Pack

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

Chapter Binary Representation of Numbers

CPS 6 Test 1 Fall 2000 Dr. Rodger String class

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

LSP 121. LSP 121 Math and Tech Literacy II. Topics. More VBA. More VBA. Variables

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Functions and Recursion

Nested Loops. Chapter 11

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Component Pascal Syntax

DelphiScript Keywords

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Outline. What Digit? => Number System. Decimal (base 10) Significant Digits. Lect 03 Number System, Gates, Boolean Algebra. CS221: Digital Design

Lecture 06: Feb 04, Transforming Data. Functions Classes and Objects Vectorization Subsets. James Balamuta STAT UIUC

by Pearson Education, Inc. All Rights Reserved. 2

EAS230: Programming for Engineers Lab 1 Fall 2004

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

C: How to Program. Week /Mar/05

PROBLEM SOLVING TECHNIQUES SECTION - A. 1. Answer any ten of the following

C: How to Program. Week /Apr/23

EECS150 - Digital Design Lecture 13 - Combinational Logic & Arithmetic Circuits Part 3

Input and Expressions Chapter 3 Slides #4

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Programming in C++ 5. Integral data types

Expression and Operator

Quick Reference Guide

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 2 - Introduction to C Programming

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

UCT Algorithm Circle: Number Theory

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

VB CONSOLE SUMMER WORK. Introduction into A Level Programming

Arithmetic and Bitwise Operations on Binary Data

Chapter Two MIPS Arithmetic

Information Science 1

A flow chart is a graphical or symbolic representation of a process.

UNIT- 3 Introduction to C++

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Quick Reference Guide

Transcription:

B B Chapter 14 Random Numbers

293 DEFINITION PboxRandom; CONST seedlimit = 2147483647; Figure 14.1 The interface of module PboxRandom. PROCEDURE Int (n: INTEGER): INTEGER; PROCEDURE Randomize; PROCEDURE Real (): REAL; PROCEDURE SetSeed (n: INTEGER); END PboxRandom.

PROCEDURE SetSeed (n: INTEGER); Pre 0 < n 20 n < seedlimit 21 Post The random number seed is initialized to n. 294 PROCEDURE Randomize Post The random number seed is initialized to a value derived from the system clock. Two calls to Randomize should be separated by more than one second to guarantee different values of seed. PROCEDURE Real (): REAL; Post Returns a random real between 0.0 and 1.0.

294 MODULE Pbox14A; IMPORT Dialog, PboxRandom, PboxStrings, StdLog; VAR d*: RECORD seed*: INTEGER; END; Figure 14.2 A procedure that prints ten random real numbers to the Log. PROCEDURE SetSeed*; IF (0 < d.seed) & (d.seed < PboxRandom.seedLimit) THEN PboxRandom.SetSeed(d.seed) ELSE StdLog.String("Seed must be greater than 0 and less than 2147483647."); StdLog.Ln END END SetSeed; PROCEDURE Randomize*; PboxRandom.Randomize END Randomize;

PROCEDURE Display*; VAR i: INTEGER; x: REAL; realstring: ARRAY 8 OF CHAR; FOR i := 1 TO 10 DO StdLog.String("i = "); StdLog.Int(i); x := PboxRandom.Real(); PboxStrings.RealToString(x, 5, 3, realstring); StdLog.String(" END; StdLog.Ln END Display; d.seed := 1 END Pbox14A. x = "); StdLog.String(realString); StdLog.Ln 295

295 Figure 14.3 The output of procedure Display of Figure 14.2.

297 Figure 14.4 The output for the procedure of Figure 14.5.

297 MODULE Pbox14B; IMPORT Dialog, PboxRandom, PboxStrings, StdLog; VAR d*: RECORD seed*: INTEGER; limit*: INTEGER; END; Figure 14.5 A procedure that prints ten random integers to the Log. PROCEDURE SetSeed*; IF (0 < d.seed) & (d.seed < PboxRandom.seedLimit) THEN PboxRandom.SetSeed(d.seed) ELSE StdLog.String("Seed must be greater than 0 and less than 2147483647."); StdLog.Ln END END SetSeed; PROCEDURE Randomize*; PboxRandom.Randomize END Randomize;

297 PROCEDURE Display*; VAR i: INTEGER; m: INTEGER; IF (0 < d.limit) & (d.limit < PboxRandom.seedLimit) THEN FOR i := 1 TO 10 DO m := PboxRandom.Int(d.limit); StdLog.Int(m) END; StdLog.Ln ELSE StdLog.String("Limit must be greater than 0 and less than 2147483647."); StdLog.Ln END END Display; d.seed := 1; d.limit := 0 END Pbox14B.

PROCEDURE Int (n: INTEGER): INTEGER; Pre 0 < n 20 n < seedlimit 21 Post Returns a random integer in the range 0..n-1. 298

REPEAT Statement1 UNTIL Condition1 298 S1 F C1 T Figure 14.6 The flowchart for the REPEAT statement.

299 Figure 14.7 The output for the procedure in Figure 14.8.

300 MODULE Pbox14C; IMPORT Dialog, PboxRandom, PboxStrings, StdLog; VAR d*: RECORD seed*: INTEGER; END; Figure 14.8 A procedure that simulates rolls of a pair of dice. PROCEDURE SetSeed*; IF (0 < d.seed) & (d.seed < PboxRandom.seedLimit) THEN PboxRandom.SetSeed(d.seed) ELSE StdLog.String("Seed must be greater than 0 and less than 2147483647."); StdLog.Ln END END SetSeed; PROCEDURE Randomize*; PboxRandom.Randomize END Randomize;

PROCEDURE RollDice*; VAR die1, die2: INTEGER; sum, numrolls: INTEGER; numrolls := 0; StdLog.String("Rolls of the dice"); StdLog.Ln; REPEAT die1 := PboxRandom.Int(6) + 1; die2 := PboxRandom.Int(6) + 1; INC(numRolls); sum := die1 + die2; StdLog.Int(die1); StdLog.Int(die2); StdLog.Ln UNTIL (sum = 7) OR (sum = 11); StdLog.String("You rolled "); StdLog.Int(numRolls); StdLog.String(" times before you got 7 or 11."); StdLog.Ln END RollDice; d.seed := 1 END Pbox14C. 300

z n + 1 = az n MOD m 301 Example If you select values of (17, 5) for (m, a), and the current value of seed z n is 11, then the next seed is computed as z n + 1 = az n mod m = 5 11 mod 17 = 55 mod 17 = 4 The 20 successive seed values from the (17, 5) Lehmer generator are 11 4 3 15 7 1 5 8 6 13 14 2 10 16 12 9 11 4 3 15 with an initial seed of 11.

Example The (17, 13) Lehmer generator produces the sequence 4 1 13 16 4 1 13 16 4 1 13 starting from 4. 301

302 MODULE PboxRandom; IMPORT Dates; CONST multiplier = 48271; modulus = 2147483647; quotient = modulus DIV multiplier; remainder = modulus MOD multiplier; seedlimit* = modulus; VAR seed: INTEGER; Figure 14.9 An implementation of the standard (2147483647, 48271) Lehmer random number generator. PROCEDURE ComputeNextSeed; VAR low, high: INTEGER; low := seed MOD quotient; high := seed DIV quotient; seed := multiplier * low - remainder * high; IF seed <= 0 THEN seed := seed + modulus END END ComputeNextSeed;

PROCEDURE Int* (n: INTEGER): INTEGER; ASSERT(0 < n, 20); ASSERT(n < seedlimit, 21); ComputeNextSeed; RETURN SHORT(ENTIER(seed / modulus * n)) END Int; 302 PROCEDURE Randomize*; VAR date: Dates.Date; time: Dates.Time; i: INTEGER; Dates.GetDate(date); Dates.GetTime(time); seed := 86400 * Dates.Day(date) + 3600 * time.hour + 60 * time.minute + time.second; (* Elapsed time this year in seconds *) FOR i := 0 TO 7 DO ComputeNextSeed END END Randomize;

PROCEDURE Real* (): REAL; ComputeNextSeed; RETURN seed / modulus END Real; PROCEDURE SetSeed* (n: INTEGER); VAR i: INTEGER; ASSERT(0 < n, 20); ASSERT(n < seedlimit, 21); seed := n MOD modulus; FOR i := 0 TO 7 DO ComputeNextSeed END END SetSeed; Randomize END PboxRandom. 302

304 0.0 0.2 0.4 0.6 0.8 1.0 Figure 14.10 The transformation PboxRandom.Int makes from the real number line to the integer number line. 0 1 2 3 4