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-

Slides:



Advertisements
Similar presentations
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
 2003 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS150 Introduction to Computer Science 1
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Modeling and Simulation Random Number Generators
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?
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
© 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.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
R ANDOM N UMBER G ENERATORS Modeling and Simulation CS
C++ Programming Lecture 12 Functions – Part IV
CSE202: Lecture 13The Ohio State University1 Function Scope.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
while Repetition Structure
CISC181 Introduction to Computer Science Dr
Why they aren't really random
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Random Number Generators
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
Value returning Functions
Arrays Kingdom of Saudi Arabia
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Arrays Arrays A few types Structures of related data items
Fundamental Programming
 Is a machine that is able to take information (input), do some work on (process), and to make new information (output) COMPUTER.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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- srand

algorithmic (software) generators Algorithmic generators are widely accepted because they meet all of the following criteria. randomness - output passes all reasonable statistical tests of randomness controllability - able to reproduce output, if desired portability - able to produce the same output on a wide variety of computer systems efficiency - fast, minimal computer resource requirements documentation - theoretically analyzed and extensively tested

Rand function It’s advantage that it produce the same number. They solve this drawback by using (srand) function

Code by (rand) function 1 2 // Shifted, scaled integers produced by 1 + rand() % 6. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; #include // contains function prototype for rand int main() 15 { 16 // loop 20 times 17 for ( int counter = 1; counter <= 20; counter++ ) { // pick random number from 1 to 6 and output it 20 cout << setw( 10 ) << ( 1 + rand() % 6 ); // if counter divisible by 5, begin new line of output 23 if ( counter % 5 == 0 ) 24 cout << endl; } // end for structure

27 28 return 0; // indicates successful termination } // end main

(Srand) function It used to solve the disadvantage of (rand) function Advantage of (srand) is: 1- every time it run it produce different number The disadvantage of (srand) is: 1- the user must enter the variable to start generator.

Code by (srand) function

Develop to use (srand) function To prevent the user from entering the variable they make enhancement to (srand) by using (srand(time(0))) To use this function we must add the library (time.h)

Srand(NULL)function

Advantage: Every time generate random number without intervention from the user