Monday, June 11, 2007

Simulating the throw of dice in C++

What makes the throw of dice special is the fact that you can’t always guess which number will come up. This property is known as randomness, which is needed in certain types of programs (ex. Games).

In this article, we are going to discuss how random numbers are generated and used in C++ with the help of a simple example.

The program illustrates generation and use of random numbers in C++:

  //C++ Program to generate RANDOM NUMBERS
//function is isolated so it may be easily
//incorporated in other programs
//NOTE:stdlib.h header file is required
//for using this funtion elsewhere


int random(int HighestNumber,int LowestNumber);

void main(void)
char ch = '';
puts("Random Number Generator: ");

while(ch!='q' && ch!='Q')
//loop until q or Q is pressed
puts("Press any key to throw dice...");

//%d tells the compiler that the value after
//the comma is of integer type (value returned
//by the function)

puts("\nPress Q to QUIT");


//it takes two integer arguments and returns a
//random number in between the two.

int random(int HighestNumber,int LowestNumber)
//rand() is a standard library function
//which generates random numbers, these are
//operated to get the numbers within range

//stdlib.h header file is necessary for its
int i=((rand()%(HighestNumber-LowestNumber+1))+LowestNumber);
return i;

Hope this article helps!


  1. Thank you for the example.

    One of our members posted this code on our C and C++ forum and asked for help. I wanted list the problems with the code as a feedback:

    1) Although it can be compiled as C++, the code is strictly in C

    2) 'void main' is illegal; it must be 'int main'

    3) using the uninitialized 'ch' in the while loop has undefined behavior

    4) the parameter names of the 'random' function does not match its semantics; one would expect the function to produce results in the set {3,4,5} when called as random(5,3); unfortunately, as implemented, it produces results in the set {3,4,5,6,7}

    5) arguably it is more natural to swap the arguments of 'random' to take the lowest number before the highest number as in

    int random(int LowestNumber, int HighestNumber);

    6) although not strictly necessary, it might be nice to demonstrate srand() along with rand()

    7) it might be better to demonstrate without using the nonstandard function getch().

    Thank you...

  2. @Ali Çehreli

    Thank you for the comment. I'm replying point-wise:

    2. 'void main' isn't illegal, it just isn't according to the standards.

    3. Yes 'ch' should be initialized to '' or something.

    4. The function itself is implemented wrongly. It should be:

    int i=((rand()%(HighestNumber-LowestNumber+1))+LowestNumber);

    That way it'll return values accordingly. I'm also changing the code above.


You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.