Random Number Generation

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Exercise 2.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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 C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
CSE202: Lecture 13The Ohio State University1 Function Scope.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Monte Carlo Methods Some example applications in C++
C++ Programming: Presentation 1
Lecture 11 Multi-dimensional Arrays
#define #include<iostream> using namespace std; #define GO
Why they aren't really random
Control Statement tsenghy.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Command Line Arguments
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
CMPT 201 Functions.
Programming Fundamentals
Lab Session-9 CSIT-121 Spring 2005
Pointers Psst… over there.
Chapter 4 Loops Case Studies
بسم الله الرحمن الرحيم.
Reserved Words.
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
توابع در C++ قسمت اول اصول كامپيوتر 1.
Starting Out with C++: From Control Structures through Objects
More About File Reading
Chapter 5 Function Basics
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
اصول کامپیوتر ۱ مبانی کامپیوتر و برنامه‌سازی
Code::Block vs Visual C++
Iterative Constructs Review
Lab 1 Introduction to C++.
Mr. Dave Clausen La Cañada High School
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Glenn Stevenson CSIS 113A MSJC
Statements and flow control
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CS 144 Advanced C++ Programming January 31 Class Meeting
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Pointer Data Type and Pointer Variables
Programming Strings.
Lecture 9 Files Handling
Introduction to Algorithms and Programming COMP151
CSE Module 1 A Programming Primer
Programming Fundamental
Presentation transcript:

Random Number Generation

Pseudo-Random Generation

rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }

rand() First Execution #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844

rand() First Execution Second Execution #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844 38457 733887 7384 94877 243 3994452 374008 23146 11833432 237844

srand() #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }

srand() First Execution #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 464 53735 342 23 6578 889 93723 7165 7422457 78614

srand() First Execution Second Execution #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; } 464 53735 342 23 6578 889 93723 7165 7422457 78614 6877 245768 215 57618 78511 79738 3461 175117 35 257868

limiting range: [0, MAX_RAND] #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { long next_value; srand(time(NULL)); for (int i = 1; i <= 100000; i++) if (rand()%100 <= 34) cout << "hello" << endl; // ~35,000 times else cout << "goodbye" << endl; // ~65,000 times return 0; }

End of Session