Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dry run Fix Random Numbers

Similar presentations


Presentation on theme: "Dry run Fix Random Numbers"— Presentation transcript:

1 Dry run Fix Random Numbers
Computer Science I: Dry run Fix Random Numbers

2 Learning Objectives Be able to dry run a program that uses a for loop.
Get out your notes. Be able to dry run a program that uses a for loop. Be able to fix a program that uses a for loop. Be able to read a program that uses random numbers. Be able to write a program that uses random numbers.

3 DIV Pascal’s Integer Division
How can you get an integer result when you divide? For example, if you divide 5 by 2 what do you think the answer would be? What about the remainder. Pascal has math operations for finding the result of integer division and the remainder of integer division.

4 DIV and MOD DIV is used to find the result of integer division
For example X := 13 DIV 5; X:= 21 DIV 10; X:= 20 – 8 DIV 3; x:= 1 DIV 2; MOD is used to find the remainder of integer division X := 13 MOD 5; X:= 21 MOD 10; X:= 20 – 8 MOD 3; x:= 1 MOD 2;

5 DIV and MOD samples Ans:=23 DIV 10; Ans:= 14 DIV 4; Ans:= 52 DIV 5;
Ans:= 23 MOD 10; Ans:= 14 MOD 4; Ans:= 52 MOD 5; Ans:= 14 MOD 3; Ans:= 8 MOD 9; Ans:= 5 MOD 10; Ans:= 4.13 MOD 2;

6 Dry Run the Following

7 This is fixyng.pas on the class website.

8 Random Numbers…why use them?
Testing data Modeling Gaming Adding ‘chance’ to something

9 How do you do it? Seed the computer’s random number generator
Randomize; {Command} Make the computer generate a random number in a given range. Random command Number:= random(12)+ 2;

10 Syntax Answer:= random(range) + offset;
Coin:= random(2)+1; Die:= random(6)+1; Answer:= 2*random(range) + offset; Evans/Odds Evento100:= 2*random(50)+2; Oddto99:=2*random(50)+1

11 Example Program program flipper; var count, coin:integer; begin randomize; for count := 1 to 50 do coin:= random(2) + 1; {1 = heads, 2 = tails} if coin = 1 then writeln('Heads') else writeln('Tails'); end; {Of the for loop} end. {Of the program}

12 Reading code Name the range of values that the following could generate Ans:= random(5); Ans:= random(6)+2; Ans:= random(40)-7; Ans:= 2*random(10)+3; Ans:= 2*random(9)-7;

13 Writing code Write the random line to generate a number for the following ranges. 1-6 2-30 A pair of 6-sided dice 3-9 odd 12-24 even

14 Check for Understanding: Find the range of values the following code produces.
1) ans:=random(6)+1; 2) ans:=random(14)+2; 3) ans:=random(7)-4; 4) ans:=random(12)+5; 5) ans:=random(23)+8; 6) ans:=random(50)-5; 7) ans:=random(18)+370; 8) ans:=2* random(10) + 1; 9) ans:=2* random(9) + 6; 10) ans:=2* random(21) + 10; 11) ans:=2* random(100) + 51;

15 Check for Understanding: Write the line of code to generate the following ranges of values
8 to 24 even 7 to 35 odd -7 to 21 odd -10 to 10 even A pair of six sided dice

16 Program options If you are stuck… Hands on, Pseudo-code, Dry run,
Flip a coin 100 times, show the total number of heads and tails AND which occurred the most often. Roll a pair of 6 sided dice 100 times. Find out which occurs most often, 3 or 11. Show how often each of these rolls occur and which occurs most often. Math tutor: User inputs the number of sample addition problems o show. The computer generates the problems and shows them to the user. The user guesses the answers At the end the computer shows how many the user answered correctly.


Download ppt "Dry run Fix Random Numbers"

Similar presentations


Ads by Google