CSC 107 - Programming for Science Lecture 22: Random Numbers & Macros.

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Advertisements

Computer Programming w/ Eng. Applications
Computability and Complexity 20-1 Computability and Complexity Andrei Bulatov Random Sources.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
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.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
CSC 107 – Programming For Science. Announcements.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 162 Visual Basic I Programming. Randomizing and Formatting Randomizing Formatting –User-Defined Formats –Named Numeric Formats.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Some Uses of Probability Randomized algorithms –for CS in general –for games and robotics in particular Testing Simulation Solving probabilistic problems.
By the end of this session you should be able to...
CPS120: Introduction to Computer Science
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
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?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
LECTURE 40: SELECTION CSC 212 – Data Structures.  Sequence of Comparable elements available  Only care implementation has O(1) access time  Elements.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
CSC Programming for Science Lecture 4: Beginning Programming.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Random numbers in C++ Nobody knows what’s next....
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.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
+ 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.
UNIT 11 Random Numbers.
Controlling Program Flow with Decision Structures.
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
CSC Programming for Science Lecture 37 : Course Review.
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC Programming for Science Lecture 18: More Data Files.
CSC Programming for Science Lecture 23: More on Function Parameters.
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.
Ray Karol 2/26/2013. Let’s Make a Deal Monte Hall Problem Suppose you’re on a game show, and you’re given a choice of three doors: Behind one door is.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Why they aren't really random
Building Java Programs
ECE Application Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Iterative Constructs Review
CMPT 201 Functions.
Lexical Elements, Operators, and the C Cystem
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lexical Elements, Operators, and the C Cystem
Random number generators
6 Functions.
Presentation transcript:

CSC Programming for Science Lecture 22: Random Numbers & Macros

Question of the Day On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door, the host, opens another door which has a goat. He then asks, "Do you change your selection?” Should you?

Proving This Solution After 1 experiments Sticking it out: % correct Switching doors: 0.00% correct After 1000 experiments Sticking it out: 34.10% correct Switching doors: 68.20% correct After experiments Sticking it out: 33.40% correct Switching doors: 66.72% correct

Pseudo-random Numbers Computers are stupid & only follow orders  Cannot be random or do unexpected things Algorithm generates sequence of integers  No correlation between numbers in sequence  (One hopes) Sequence should be very long Numbers completely predictable  Just have to know the algorithm  Or wait long enough to see patterns repeat

Pseudo-random Numbers

Seeding Pseudo-random number generators rely upon a seed  Value used to start sequence of random numbers A seed always yields identical sequence of random numbers  Useful for testing & debugging code  Bad when comparing runs Usually seed with current time: srand(times(NULL));

rand() Pre-defined function  Prototype in stdlib.h Generates integer between 0 & RAND_MAX  RAND_MAX also from stdlib.h To generate a random number…  … between 0 - 5: (rand() % 6)  … between : (rand() % 6) + 5  … between : (rand() % 6) - 5  … between : (rand() % 51) - 25

Results For Staying correct = 0 for (i = 0; i < experiments; i++) { guess = rand() % 3 solution = rand() % 3 if (guess == solution) { correct += 1; } pct = correct /((double)experiments) * 100;

Results For Switching correct = 0 for (i = 0; i < experiments; i++) { guess = rand() % 3 solution = rand() % 3 if (guess != solution) { /* Other incorrect solution revealed, so switch to correct solution */ correct += 1; } pct = correct /((double)experiments) * 100;

Warning!!!! Many systems have bad rand() versions  Generates random numbers, …  … but randomness limited to largest digits So, remainders will not be that random  Recommend that they not be used Instead round random decimal numbers  Decimal numbers use those largest digits

Random Decimal Numbers No function generates random decimals  Just use rand() / (RAND_MAX + 0.0) To generate random decimal number…  … between 0 - 1: rand()/(RAND_MAX+0.0)  … between 0 - 5: rand()/(RAND_MAX+0.0)*5  … between : rand()/(RAND_MAX+0.0)*-5  … between : (rand()/(RAND_MAX+0.0)*50)-25

Symbolic Constants Pre-processor replaces name with value  Compiler only sees the constant value  Programmer only sees the name  Makes code far easier to read, write, debug Names traditionally in all CAPITAL letters

What You Write And Work With #define PI #define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = PI * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE);

#define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE); #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE); #define DUMB_EXAMPLE 10.5 area = * (r * r); printf(“%lf”, 10.5); printf(“%lf”, DUMB_EXAMPLE); area = * (r * r); printf(“%lf”, 10.5); printf(“%lf”, 10.5); What The Compiler Sees #define PI #define AVOGADRO 6.022E23 #define MY_SHOE_SIZE 10.5 #define DUMB_EXAMPLE MY_SHOE_SIZE area = * (r * r); printf(“%lf”, MY_SHOE_SIZE); printf(“%lf”, DUMB_EXAMPLE);

Macros Powerful cousins of symbolic constants Shares similar traits with their relatives  Begin with #define  Must be only code on line (and fit one 1 line)  Name usually in all CAPITAL  Text replaced before compilation But can use parameters Value should be placed in parenthesis

Creating a Macro Macro can be very simple #define RAND_DEC (rand()/(RAND_MAX+0.0)) Macros can also take parameters #define SQUARED(x) (x * x) #define VELOCITY(init,a,t) (init+(a*t)) #define RAND_X(x) (rand() % x) #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y) ? x : y) Parameters replaced during compilation  This will cause problems in code, be careful!

What You Write And Work With #define RAND_DEC (rand()/(RAND_MAX+0.0)) #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y)? x : y) int i = RAND_RNG(4, 7); double rnd = RAND_DEC; int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++));

What the Compiler Sees #define RAND_RNG(lo,hi) ((rand()%(hi- lo))+lo) #define MAX(x,y) ((x > y)? x : y) int i = RAND_RNG(4, 7); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++)); #define MAX(x,y) ((x > y)? x : y) int i = ((rand()%(7-4))+4); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, MAX(i, j++)); int i = ((rand()%(7-4))+4); double rnd = (rand()/(RAND_MAX+0.0)); int j = 6; printf(“%lf”, rnd); printf(“%d”, (i > j++)? i : j++);

Your Turn Try doing following problems on your own

For Next Lecture Continue programming assignment #2  Now due next Wednesday – 11/01/06 Finish week #9 weekly assignment