LectureTwelve - Lecture 12 Functions – Part II By Ghada...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 12 Functions – Part II By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department The Hashemite University 2 Outline ¡ Random number generation in C++. ¡ enum data type identification and usage. The Hashemite University 3 Random Number Generation I ¡ Random number generation is used mainly in simulation and game playing based application. ¡ The rand() function is used in C++ to generate random integer numbers between 0 and a maximum specified value. ¡ rand() function takes nothing (void) as its arguments and returns an unsigned integer. ¡ In order to use this function you must Load <cstdlib> or <stdlib.h> The Hashemite University 4 Random Number Generation II ¡ rand function syntax: int i = rand(); ¡ Generates a pseudorandom number between and RAND_MAX (usually 32767) ¡ RAND_MAX is a symbolic constant defined in the stdlib header file. ¡ 0 <= rand() <= RAND_MAX. ¡ A pseudorandom number is a preset sequence of "random" numbers. ¡ The same sequence is generated upon every program execution. ¡ This repeated behavior is essentially in programming debug and verification in simulation and other random-based applications. The Hashemite University 5 Random Number Generation III ¡ srand function ¡ Jumps to a seeded location in a "random" sequence. ¡ Similar to rand() function, srand function is defined in the <stdlib.h> library. ¡ Takes an unsigned integer as a seed (i.e. as an argument). ¡ It does not return any value (returns void), it just change the random sequence (randomizing the rand() function). ¡ Can be called more than once within the same program. ¡ Still you need to use the rand() function to get the random numbers. The Hashemite University 6 Random Number Generation IV ¡ srand syntax: srand( seed ); ¡ seed can be any unsigned integer entered manually be the user or initialized through the program. ¡ If the same seed is used every time the program is run we will get the same random sequence (i.e. the same without seed). The Hashemite University 7 Random Number Generation V ¡ To initialize seed value automatically use the following syntax: srand( time( 0 ) ); ¡ time( 0 ) ¡ Returns the current calendar time in seconds....
View Full Document

This note was uploaded on 10/29/2010 for the course COMPUTER E 2010 taught by Professor Ghadaal-mashaqbeh during the Spring '10 term at Hashemite University.

Page1 / 23

LectureTwelve - Lecture 12 Functions – Part II By Ghada...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online