Presentation is loading. Please wait.

Presentation is loading. Please wait.

Revision.

Similar presentations


Presentation on theme: "Revision."— Presentation transcript:

1 Revision

2 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

3 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

4 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


Download ppt "Revision."

Similar presentations


Ads by Google