Random Bricks.

Slides:



Advertisements
Similar presentations
Simple Probability and Odds
Advertisements

Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Mathematics in Today's World
Math notebook, pencil, and possibly calculator. Definitions  An outcome is the result of a single trial of an experiment.  The sample space of an experiment.
AP Statistics Section 6.2 A Probability Models
1 Copyright M.R.K. Krishna Rao 2003 Chapter 5. Discrete Probability Everything you have learned about counting constitutes the basis for computing the.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Chapter 6 Probability.
What are the chances of that happening?. What is probability? The mathematical expression of the chances that a particular event or outcome will happen.
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Bell Work Suppose 10 buttons are placed in a bag (5 gray, 3 white, 2 black). Then one is drawn without looking. Refer to the ten buttons to find the probability.
EXIT NEXT Click one of the buttons below or press the enter key BACKTOPICSProbability Mayeen Uddin Khandaker Mayeen Uddin Khandaker Ph.D. Student Ph.D.
1.3 Simulations and Experimental Probability (Textbook Section 4.1)
PROBABILITY.
1.4 Equally Likely Outcomes. The outcomes of a sample space are called equally likely if all of them have the same chance of occurrence. It is very difficult.
Basic Concepts of Probability
Bell Work/Cronnelly. A= 143 ft 2 ; P= 48 ft A= 2.3 m; P= 8.3 m A= ft 2 ; P= 76 ft 2/12; 1/6 1/12 8/12; 2/3 6/12; 1/2 0/12 4/12; 1/3 5/12 6/12; 1/2.
What is the probability of two or more independent events occurring?
3/7/20161 Now it’s time to look at… Discrete Probability.
How likely is something to happen..  When a coin is tossed, there are two possible outcomes: heads (H) or tails (T) We say the probability of a coin.
Warm up Given the data points, create a stem and leaf plot and a box and whisker plot: 3, 5, 11, 34, 28, 19, 4, 6, 14, 17, 22, 30, 1, 1, 9, 10, 24, 27,
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Probability.
Experimental and Theoretical (Simple and Compound) Jeopardy
Now it’s time to look at…
Monte Carlo Methods Some example applications in C++
ICS 253: Discrete Structures I
Probability.
Experimental Probability vs. Theoretical Probability
Bell Work.
Probability.
Probability.
Bring a penny to class tomorrow
© 2016 Pearson Education, Ltd. All rights reserved.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
CS104:Discrete Structures
Basic Concepts of Probability
Organization of Programming Languages
Conditional Probability
Probability The term probability refers to indicate the likelihood that some event will happen. For example, ‘there is high probability that it will rain.
5.1 Probability of Simple Events
Lesson Probability Students will be able to understand the concept of probability and the relationship between probability and likelihood. Students.
Tuesday, August 25, 2015 DO NOW On the opener sheet that you picked up, respond to the following questions in the “Tuesday” box Imagine that you have.
Now it’s time to look at…
Probability.
Probability Probability is a measure of how likely an event is to occur. For example – Today there is a 60% chance of rain. The odds of winning the lottery.
PROBABILITY.
An aggregation mechanism
Welcome stand quietly * take out your math folder *Warm-Up Out
Chapter 3 Probability Larson/Farber 4th ed.
Now it’s time to look at…
PROBABILITY.
Probability.
Probability.
Simulation Discrete Variables.
Lecture 2 Binomial and Poisson Probability Distributions
Now it’s time to look at…
Probability.
Applied Discrete Mathematics Week 12: Discrete Probability
Now it’s time to look at…
5-8 Probability and Chance
Topic: Introduction to Probability
STAND QUIETLY.
Probability.
Probability.
Probability.
Lecture 2 Basic Concepts on Probability (Section 0.2)
Chapter 11 Probability.
Sets, Combinatorics, Probability, and Number Theory
Presentation transcript:

Random Bricks

Level 3 - Reading https://bricklayerdotorg.wordpress.com/generating-random-bricks/ Random Bricks:

Random Numbers

Random Numbers In this discussion we will ignore the idiosyncrasies surrounding how computers represent real numbers and issues surrounding equality comparisons. Virtually all programming languages provide a variety of ways to generate a random (real) number that, mathematically speaking, is in the range 0.0 ≤ n < 1.0 Note that, in this range, n can never equal 1.0, but can equal 0.0.

A Random Number Function To get a random number, one typically calls a special function. The basic random number function is a nullary function. Its name can vary from language to language, but the function is typically called random or rand. Let random denote a function that when called generates a random number in the range 0.0 ≤ n < 1.0 Let us assume that random is a nullary function which can therefore be called as follows: random ()

Simulating a Coin Flip

An Algorithm The following algorithm shows how the random function can be used to simulate a coin flip. In the algorithm below, H stands for “heads” and T stands for “tails”. Let n = random () if n < 0.5 then print (“H”) else print (“T”) This algorithm can be easily implemented in SML. An important question is: What kind of output could one expect from a program that flipped a coin, say, 10 times? Would you expect 5 heads and 5 tails? Would you expect 10 heads and 0 tails?

An Experiment I conducted an experiment where I flipped an actual coin 10 times and got the following sequence of heads (H) and tails (T). H T T T T T H H H H One important thing to note about the sequence is that random does not mean “evenly distributed”. For example, after seeing a sequence of 3 tails, it is incorrect to assume that the next coin flip must yield heads. What would happen if I ran the experiment again? What would be the chances (i.e., the odds) that same sequence of heads and tails would occur?

Properties of a Random Function Let us consider writing a program that conducts the coin- flipping experiment on our behalf. What would you want to have happen if you ran such a program twice? Would you want the program to produce the same output every time it is executed? If not, how might this impact your ability to test the program? Would you want the program to produce the same output if it is executed on a different computer?

Simulating the Roll of a Die

An Algorithm The following algorithm shows how the random function can be used to simulate the roll of a six- sided die. The basic idea here is to partition the  range 0.0 ≤ n < 1.0 into six equal sections and associate each section with (aka, map each section to) a side of the die. Let n = random ()        if 0.0/6.0 ≤ n < 1.0/6.0 then print (“1”) else if 1.0/6.0 ≤ n < 2.0/6.0 then print (“2”) else if 2.0/6.0 ≤ n < 3.0/6.0 then print (“3”) else if 3.0/6.0 ≤ n < 4.0/6.0 then print (“4”) else if 4.0/6.0 ≤ n < 5.0/6.0 then print (“5”) else if 5.0/6.0 ≤ n < 6.0/6.0 then print (“6”) This algorithm can also be easily implemented in SML.

An Experiment I conducted an experiment where I rolled an actual die 10 times and got the following sequence of numbers. 3 5 1 2 1 5 2 2 5 2 Note that the numbers 4 and 6 never appeared. Does this imply that the die is “loaded”? No. If I roll the die five more times, will I be guaranteed that 4 and/or 6 will appear? No. It is important to appreciate that random number sequences have such properties.

Bricklayer A Random Brick Function

The generateRandomBrickFn function Bricklayer provides a function, called generateRandomBrickFn, that can be used to generate random bricks. This function implements an algorithm similar to the coin-flipping and die-rolling algorithms described previously. Let brickList denote a list of bricks. The evaluation of the expression generateRandomBrickFn brickList will produce a nullary function value as its result. A val-declaration can be used to bind a variable (i.e., our desired function name) to this nullary function value as follows. val myName = generateRandomBrickFn brickList; The evaluation of the function call “myName ()” will return a brick, randomly selected from brickList.

Predefined Brick Lists Bricklayer provides a set of predefined brick lists that can be used to generate random brick functions. This set includes the following brick lists. grayscale greenScale blueScale purpleScale redScale warmScale brownScale clearScale allOneBitBricks

Example 1a A one dimensional sequence of RED and BLACK bricks.

Question: In what order are these bricks “put”? open Level_3; val brickList = [BLACK,RED]; val randomBrick = generateRandomBrickFn brickList; fun sequence0 (x,z) = let val delta = 1; val brick1 = randomBrick (); val brick2 = randomBrick (); in put2D (1,1) brick1 (x + 0 * delta, z); put2D (1,1) brick2 (x + 1 * delta, z) end; fun sequence1 (x,z) = val delta = 2*1; sequence0 (x + 0 * delta, z ); sequence0 (x + 1 * delta, z ) fun sequence2 (x,z) = val delta = 2*2*1; sequence1 (x + 0 * delta, z ); sequence1 (x + 1 * delta, z ) fun sequence3 (x,z) = let val delta = 2*2*2*1; in sequence2 (x + 0 * delta, z ); sequence2 (x + 1 * delta, z ) end; fun sequence4 (x,z) = val delta = 2*2*2*2*1; sequence3 (x + 0 * delta, z ); sequence3 (x + 1 * delta, z ) build2D (32,32); sequence4 (0,0); show2D "random in 1D"; Question: In what order are these bricks “put”? Question: How can we confirm this?

Example 1b Tracing a one dimensional sequence of RED and BLACK bricks.

fun showPoint (x,z) = let val pointStr = "(" ^ Int.toString x ^ "," ^ Int.toString z ^ ")"; in print("\n(x,z) = " ^ pointStr) end; fun myPut dim brick p = ( showPoint p; put2D dim brick p ); fun sequence0 (x,z) = val delta = 1; val brick1 = randomBrick (); val brick2 = randomBrick (); myPut (1,1) brick1 (x + 0 * delta, z); myPut (1,1) brick2 (x + 1 * delta, z) fun random1D p = ( print "\n\n"; sequence4 (0,0); print "\n\n" ); build2D (32,32); random1D (0,0); show2D "random in 1D";

Example 2 A four-colored two dimensional random brick sequence.

open Level_3; val brickList = [RED, GREEN, YELLOW, BLUE]; val randomBrick = generateRandomBrickFn brickList; fun board0 (x,z) = let val delta = 1; val brick1 = randomBrick (); val brick2 = randomBrick (); val brick3 = randomBrick (); val brick4 = randomBrick (); in put2D (1,1) brick1 (x , z ); put2D (1,1) brick2 (x + delta, z ); put2D (1,1) brick3 (x + delta, z + delta); put2D (1,1) brick4 (x , z + delta) end; fun board1 (x,z) = val delta = 2*1; board0 (x , z ); board0 (x + delta, z ); board0 (x + delta, z + delta); board0 (x , z + delta) fun board2 (x,z) = let val delta = 2*2*1; in board1 (x , z ); board1 (x + delta, z ); board1 (x + delta, z + delta); board1 (x , z + delta) end; fun board3 (x,z) = val delta = 2*2*2*1; board2 (x , z ); board2 (x + delta, z ); board2 (x + delta, z + delta); board2 (x , z + delta) build2D (32,32); board3 (0,0); show2D "random 2D";

Question: In what order are these bricks “put”? Question: How can we confirm this?

The End