 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:

Slides:



Advertisements
Similar presentations
Header, Specification, Body Input Parameter List Output Parameter List
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.
Probability Predictions Ch. 1, Act. 5. Probability The study of random events. Random events are things that happen without predictability – e.g. the.
Advanced LABVIEW EE 2303.
A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
 A. A math teacher gave her class two tests. 25% of the class passed both tests and 42 % of the class passed the first test. What percent of those.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
1 Copyright M.R.K. Krishna Rao 2003 Chapter 5. Discrete Probability Everything you have learned about counting constitutes the basis for computing the.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
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.
Monday, 11/18/02, Slide #1 CS 106 Intro to CS 1 Monday, 11/18/02  QUESTIONS??  Today:  Hand back, discuss HW #4  Discussion of Lab 10  Exam #2 Friday.
Random Number Generator. Using Software We can generate random number by: 1- table 2- hardware 3-- software Function to generate RN by SW is: 1-rand 2-
Section 5.1 What is Probability? 5.1 / 1. Probability Probability is a numerical measurement of likelihood of an event. The probability of any event is.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
Quiz Time! Clear your desk except for a pencil & calculator!
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Probability Refresher COMP5416 Advanced Network Technologies.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Random numbers in C++ Nobody knows what’s next....
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
UNIT 11 Random Numbers.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
CSE202: Lecture 13The Ohio State University1 Function Scope.
Simulation Discrete Variables. What is it? A mathematical model Probabilistic Uses the entire range of possible values of a variable in the model.
3/7/20161 Now it’s time to look at… Discrete Probability.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Introduction to programming in java Lecture 16 Random.
Monte Carlo Methods Some example applications in C++
Umm Al-Qura University
Probability 100% 50% 0% ½ Will happen Won’t happen
Probability Predictions Ch. 1, Act. 5.
Why they aren't really random
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Iterative Constructs Review
CMPT 201 Functions.
Lab Session-9 CSIT-121 Spring 2005
The Random Class and its Methods
CS005 Introduction to Programming
Probability Trees By Anthony Stones.
Expected Value.
Random Variable Random Variable – Numerical Result Determined by the Outcome of a Probability Experiment. Ex1: Roll a Die X = # of Spots X | 1.
Iterative Constructs Review
C++ Concerns: Timing Code, Random Numbers, Memory
Simulation Discrete Variables.
CS150 Introduction to Computer Science 1
C++ Concerns: Timing Code, Random Numbers, Memory
Expected Value.
Revision.
Presentation transcript:

 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises: None  New files/handouts: Random1.cpp to Random4.cpp

 Monday, 10/28/02, Slide #2 Functions for generating random numbers (well, pseudo-random)  The library contains the function rand(), which returns a random integer in the range 0 to RAND_MAX (a system-determined constant, 32,767 on our system)  See the program Random1.cpp  How do the random values generated compare if we run the program more than once?

 Monday, 10/28/02, Slide #3 Generating different numbers on each run: srand(), time(0)  Function rand() essentially uses a list of numbers that are randomly distributed: 41, 18467, 6334,...  First call to rand() always returns 41, 2nd call 18467, etc.  Function srand(int n) changes the starting point  Use srand(n) just once before using rand(): n =1 is default; other n’s give other starting points  Function time(0) returns # seconds elapsed since 1/1/70!  Use to get an arbitrary starting point for srand():  srand(time(0)) ; Use this once before rand() for “random” starting point  See Random2.cpp

 Monday, 10/28/02, Slide #4 Changing the range of random numbers with mod operator %  If Q is a positive integer, then rand() % Q is a number from 0 to Q -1  E.g. if Q = 6, then rand() % 6 = 0,1,2,3,4 or 5  rand() % Q always produces one of Q consecutive integers, starting with 0  To change the starting value from 0 to another integer S, use rand() % Q + S  E.g. if Q = 6 and S = 1, then rand() % 6 +1= 1,2,3,4, 5 or 6: essentially the toss of a die!  To get a value from loVal to (and including) hiVal:  rand() % (hiVal - loVal + 1) + loVal  See Random3.cpp

 Monday, 10/28/02, Slide #5 Some random exercises...  Write a code segment that tosses a coin (randomly outputs “heads” or “tails”)  Modify it so it cheats -- heads are twice as likely as tails  How would we generate a random probability -- a fraction between 0 and 1?  Modify code to generate heads with desired probability p between 0 and 1  See Random4.cpp