Download presentation
Presentation is loading. Please wait.
1
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game
2
2 Random Number Generation w You may generate random numbers by calling the rand() function in the cstdlib library w However, it only generates pseudorandom numbers w Check out the code on the next slide
3
3 Program Generates Random Numbers w #include w using namespace std; w void main() w { w int number; w for (int i=0; i<10; i++) w { w number = rand(); w cout<<number<<endl; w }
4
4 Scaling the Numbers w The numbers generated are too big w We may scale them down to a small range w How can we force the program to generate random numbers between 1 and 20? w
5
5 Generating Truly Random Numbers w In order to generate truly random numbers, use the srand function to provide a seed to the rand() function w This seed can also be randomized by using the current time as the seed w Include, and use srand(time(0)) before calling the rand() function
6
6 Lab Exercise Demo Required Sec01 Nov 4 th, Sec02 Nov 6 th w Design the Hi~Lo game with functions. The program should call a function to generate and return a random number below a specified limit. User’s guess will be sent to a second function to decide if it is too high or too low or just right. The program must declare the user as loser if the number could not be guessed in five attempts
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.