Revision.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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,
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
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.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.
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.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Mathematical Functions
CSE 220 – C Programming C Fundamentals.
Why they aren't really random
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.
2016.
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CMPT 201 Functions.
CS1010 Programming Methodology
Lab Session-9 CSIT-121 Spring 2005
Chapter 5 - Functions Outline 5.1 Introduction
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
CprE 185: Intro to Problem Solving (using C)
Dynamic Memory Allocation
Formatted Input/Output
Lexical Elements, Operators, and the C Cystem
The while Looping Structure
Random Number Generation
Introduction to Programming
Chapter 6 - Functions Outline 5.1 Introduction
Lexical Elements, Operators, and the C Cystem
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Assignment Operators Topics Increment and Decrement Operators
Qsort.
Header files.
Assignment Operators Topics Increment and Decrement Operators
Revision.
Pointers.
A First Book of ANSI C Fourth Edition
Conditionals.
Introduction to Programming
Week 6 CPS125.
Files.
CS150 Introduction to Computer Science 1
Formatted Input/Output
Functions.
String manipulation string.h library
Functions continued.
Loops.
Bubble sort.
Formatted Input/Output
Let’s start from the beginning
Arrays.
CS 144 Advanced C++ Programming January 31 Class Meeting
CSCE 206 Lab Structured Programming in C
The while Looping Structure
Review Lab assignments Homework #3
CSCE 206 Lab Structured Programming in C
Assignment Operators Topics Increment and Decrement Operators
The while Looping Structure
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Revision

Generating a pseudo-random number Necessary libraries: <stdlib.h> and <time.h> Seeding: srand(time(NULL)) We set the seed based on the current time NB! Time-dependent seed changes once a second. Don’t use it in a loop! Tip: use a fixed constant as seed to always test with the same set of “random” numbers Generating a random number: rand() function Function returns a random number The returned value is a positive integer The number is between 0 … RAND_MAX (the #define comes from stdlib.h library) 2018 Risto Heinsar

Sample code to get a pseudo-random number #include <stdio.h> #include <stdlib.h> #include <time.h>   int main(void) { srand(time(NULL)); // set the seed, we do this ONLY ONCE! int randNum; randNum = rand(); // call the random number function, store the return printf("max is %d\n", RAND_MAX); // largest possible random number (can’t be changed) printf(“Got: %d\n", randNum); // print out the number that rand() function returned printf(“Got: %d\n", rand()); // print out another number that rand() function returns printf(“Got: %d\n", rand() % 10); // numbers from 0 to 9 return 0; } 2018 Risto Heinsar

Lab task #1: lottery Lotto numbers should be in between 1 … 30 The user is asked for 6 numbers Program generates 10 numbers at random Display the winning numbers Check and print how many and which of the numbers matched If all 6 matched, congratulate the user on winning the jackpot Give your condolence when none of the numbers matched Advanced: make sure none of the user entered and generated numbers occur twice 2018 Risto Heinsar