MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.

Slides:



Advertisements
Similar presentations
Savitch N/A Randomness In Programming Simulations
Advertisements

1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
Programming Functions: Passing Parameters by Reference.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
An Introduction to Programming with C++ Fifth Edition
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
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.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
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-
Programming is instructing a computer to perform a task for you with the help of a programming language.
1 Lecture 3 Part 1 Functions with math and randomness.
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
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?
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
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.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
ACM-HK Local Contest 2009 Special trainings: 6:30 - HW311 May 27, 2010 (Thur) June 3, 2010 (Thur) June 10, 2010 (Thur) Competition date: June 12,
C++ Programming Lecture 10 Functions – Part II
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Variables and memory addresses
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.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 Generating Random Numbers Textbook ch.6, pg
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
CS 240 Computer Programming 1
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Pointers What is the data type of pointer variables?
Monte Carlo Methods Some example applications in C++
C++ Programming: Presentation 1
Predefined Functions Revisited
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Controlling execution - iteration
Iterative Constructs Review
Chapter 5 Function Basics
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Lab Session-9 CSIT-121 Spring 2005
توابع در C++ قسمت اول اصول كامپيوتر 1.
Random Number Generation
Chapter 5 Function Basics
Pointers & Functions.
Namespaces How Shall I Name Thee?.
Chapter 9: Value-Returning Functions
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
CHAPTER 2 Arrays and Vectors.
Pointers & Functions.
Review Lab assignments Homework #3
Presentation transcript:

MR. CRONE Generating Random Numbers

Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications  Computer Games  Statistical Analysis  Probability

Random Numbers In order to create a random number, C++ compilers provide us with the rand() function The rand() function returns a random integer that can then be assigned to a variable Example) int main(){ int myRandom = rand(); return 0; }

Random Numbers The rand() function is contained within the header file The header file must be included in order to use the rand() function #include using namespace std; int main(){ int myRandom = rand(); return 0; }

Random Numbers The code below is intended to provide us with 5 random numbers **Assume all proper header files are included int myRand; for(int i = 0; i < 5; i ++){ myRand = rand(); cout << myRand << endl; }

Random Numbers Unfortunately, the code below will continue to produce the same set of random numbers time after time To obtain new random numbers every time the program is compiled, we must use a technique called seeding int myRand; for(int i = 0; i < 5; i ++){ myRand = rand(); cout << myRand << endl; }

Random Numbers Seeding allows the program to produce a completely different set of random numbers every time the program is compiled The seeding function is srand() and this function is contained within the header file In order to properly use srand(), we must pass the following argument: time(NULL) time(NULL) is a function contained within the header file The time(NULL) function reads the computer’s internal clock time in seconds and uses this information to initialize random numbers

Random Numbers Proper use of the seeding technique is shown below The code below will now produce a new sequence of random numbers each time the source code is compiled srand(time(NULL)); int myRand; for(int i = 0; i < 5; i ++){ myRand = rand(); cout << myRand << endl; }

Random Numbers The rand() function produces a random number in the following interval: 0 <=rand() <= RAND_MAX RAND_MAX is a constant and is defined in the header file RAND_MAX = 32,767

RAND_MAX The RAND_MAX constant can be used within your own program To print RAND_MAX refer to the code below: cout << RAND_MAX << endl;