Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab Session-9 CSIT-121 Spring 2005

Similar presentations


Presentation on theme: "Lab Session-9 CSIT-121 Spring 2005"— Presentation transcript:

1 Lab Session-9 CSIT-121 Spring 2005
Random Number Generation Designing a Game

2 Random Number Generation
You may generate random numbers by calling the rand() function in the cstdlib library However, it only generates pseudorandom numbers Check out the code on the next slide

3 Program Generates Random Numbers
#include <iostream> #include <cstdlib> using namespace std; void main() { int number; for (int i=0; i<10; i++) number = rand(); cout<<number<<endl; }

4 Scaling the Numbers The numbers generated are too big
We may scale them down to a small range How can we force the program to generate random numbers between 1 and 20?

5 Generating Truly Random Numbers
In order to generate truly random numbers, use the srand function to provide a seed to the rand() function This seed can also be randomized by using the current time as the seed Include <ctime>, and use srand(time(0)) before calling the rand() function

6 Lab Exercise Demo Required Apr 6
Design the Hi~Lo game with functions. The program should call a function to generate and return a random number below 100. Player’s guess will be sent to a second function to decide if it is too high or too low or just right. Player’s statistics are recorded in a file including the guessed number followed by total number of attempts. See next two slides

7 Sample Contents of Stats File
Number Attempts =========== Mean

8 Bonus Points Bonus 50% extra points are awarded for introducing a new feature into the program. The new feature would allow a player to give up by entering a special value (for example –1). When the player gives up, that specific number and attempts are NOT recorded in the stats file.


Download ppt "Lab Session-9 CSIT-121 Spring 2005"

Similar presentations


Ads by Google