Why they aren't really random

Slides:



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

Generating Random Numbers
Random Number Generation Graham Netherton Logan Stelly.
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
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.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
Random Numbers Dick Steflik. Pseudo Random Numbers In most cases we do not want truly random numbers –most applications need the idea of repeatability.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
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.
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-
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,
Random Number Generators 1. Random number generation is a method of producing a sequence of numbers that lack any discernible pattern. 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?
Monte Carlo Methods.
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.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
 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.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
Random numbers in C++ Nobody knows what’s next....
+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
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.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 9: Value-Returning Functions
Monte Carlo Methods Some example applications in C++
Mathematical Functions
Functions and an Introduction to Recursion
Functions, Part 2 of 2 Topics Functions That Return a Value
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Lab Session-9 CSIT-121 Spring 2005
Random numbers Taken from notes by Dr. Neil Moore
The Random Class and its Methods
توابع در C++ قسمت اول اصول كامپيوتر 1.
Random Number Generation
Random-Number Generation
Pseudo-random numbers
Random vs pseudo random
Iterative Constructs Review
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
C++ Concerns: Timing Code, Random Numbers, Memory
Random number generators
CS150 Introduction to Computer Science 1
C++ Concerns: Timing Code, Random Numbers, Memory
CS 144 Advanced C++ Programming January 31 Class Meeting
6 Functions.
Divisibility Rules Dividing by 2
Review Lab assignments Homework #3
Revision.
Assignment Operators Topics Increment and Decrement Operators
Functions that return a value
Presentation transcript:

Why they aren't really random Random Numbers Why they aren't really random

Why Random? Obvious uses Less obvious Games Simulations Cryptography – shared secret

Pseudo Random Numbers Recipe: Start with number Apply random looking math transformation eg : (num * 1009) % 100 12 8 72 48 32 88 92 28 52 68

C++ Random Need C standard library: #include <cstdlib> rand(): returns value between 0 and RAND_MAX RAND_MAX defined by library, usually ~32000

Seed Seed : starting value Same seed = same sequence 12 8 72 48 32 88 92 28 52 68 12 8 72 48 32 88 92 28 52 68 12 8 72 48 32 88 92 28 52 68

Random Seed srand(num): seeds random generator with integer value num Reproducible random : seed with constant srand(12);

Random Seed Random sequence requires random seed Sources: System clock Human input Special device Lavarand Radio noise Special hardware

Time From ctime library: #include <ctime> time(0) returns seconds since Jan 1, 1970

Random Seed srand(num): seeds random generator with integer value num Reproducible random : seed with constant srand(12); Less predictable random : seed with time srand(time(0));

C++ Random Range Use % to control range: 0 to 9: int num = rand() % 10; 0 to 29: int num = rand() % 30;

C++ Random Range Range Minimum Use + NUM for lower bound 1 to 6: int num = rand() % 6 + 1; 100 to 250: int num = rand() % 151 + 100; Range Minimum

C++ Random Other Random double Divide by max size Watch out for int division double r = static_cast<double>(rand()) / RAND_MAX;

Theoretical Notes Cycles appear when we hit repeat values Use prime numbers Use larger range of values than you really need 12 8 72 48 32 88 92 28 52 68 …

Bad Tendencies Bad recipes may Cycle easily Favor part of the number space Show patterns High always followed by low

C++ Random Numbers Aren't Good Algorithm is weak Poor distribution Limited range Serious about randomness? Use an add on library