cstutoringcenter.com logo
E-mail:Password:

Become a premier member today to gain access to exclusive
member benefits! Just $5.00 to join FOR LIFE!






<< Tutorial 18
C++ Home >>

C++ Random Numbers

***Please register FREE to rate this tutorial***
Current rank: / 5 stars with 8 votes.


Topics
New libraries
Random Functions
Examples
Example 1
Example 2
Example 3

In C++, as well as any other programming language, you can generate a random number or sequence of random numbers. Here, it involves the use of two libraries of C++, <cstdlib> as well as <ctime>.

New libraries

As mentioned, you need to include two new libraries in order for you to generate a random number. Here is some information about them:

<cstdlib>
    A library to make use of the rand() and srand() functions.
    See below for a definition of them.

<ctime>
    A library that contains definitions for time and time functions
    in C++.  This is used to generate a seed for the generator.

Random Functions

All random numbers are called pseudo-random. This means that they are part of a predefined set of numbers in the language. In order for you to mix up the set each time you run a program, you need to seed the generator by making use of the srand() function.

Here is the definition of srand() and it's use:

srand( time( 0 ) )
    Will seed the generator to the current time object.
    Essentially, this will make the generator change it's
    set every time you run the program.

Now that we know how to set up the generator, we can physically generate a number by making use of the rand() function.

Here is the definition rand() and it's use:

rand()
    Will generate one single random number.  This is not
    as useful since you did not define a range for the
    numbers.

rand() % total + start;
    This line of code will give a range of numbers.  It will
    generate 1 number by calling the rand() function first, mod
    it by the total number defined and add the start
    number to it.  So say that you wanted to define the 
    numbers 1 through 15, the line to do so would be:

        rand() % 15 + 1;

    To generate the numbers from 0 to 9, simply do not 
    add any start value:

        rand() % 10; 

Here is an example program that will fill an array with random numbers:

Example 1:
Random array

Download source code here (Right click - Save Target As...)

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){
   //generates a new random set each time:
   srand(time(0));

   int arr[20] = {0};

   for(int i = 0; i < 20; i++){
      //random numbers from 1 to 10:
      arr[i] = rand() % 10 + 1;
   }

   for(i = 0; i < 20; i++){
      cout << arr[i] << " ";
   }

   return 0;
}

The program simply places random numbers from 1 to 10 inside the array called arr. It will output the values in the array in the second program. This is a simple program to see how random numbers work. Run the program multiple times to see the different numbers appear.

Example 2:
Random Counter

Download source code here (Right click - Save Target As...)

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){
   //generates a new random set each time:
   srand(time(0));

   int arr[25] = {0};

   for(int i = 0; i < 1000; i++){
      //random numbers from 1 to 10:
      arr[rand() % 25]++;
   }

   for(i = 0; i < 25; i++){
      cout << i << " was generated: " << arr[i] << " times.\n";
   }

   return 0;
}

This program will keep track of how many times a random number was drawn. This will increment the appropriate place in the array. The generated number itself will be the index of the array. One possible output can be:

0 was generated: 33 times.
1 was generated: 33 times.
2 was generated: 43 times.
3 was generated: 44 times.
4 was generated: 45 times.
5 was generated: 39 times.
6 was generated: 44 times.
7 was generated: 40 times.
8 was generated: 41 times.
9 was generated: 43 times.
10 was generated: 37 times.
11 was generated: 38 times.
12 was generated: 42 times.
13 was generated: 37 times.
14 was generated: 38 times.
15 was generated: 42 times.
16 was generated: 30 times.
17 was generated: 37 times.
18 was generated: 42 times.
19 was generated: 38 times.
20 was generated: 33 times.
21 was generated: 52 times.
22 was generated: 45 times.
23 was generated: 51 times.
24 was generated: 33 times.

The output will be different each time the program is run.

Example 3:
Random Stars

Download source code here (Right click - Save Target As...)

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){
   //generates a new random set each time:
   srand(time(0));
   
   int num = 0;

   for(int i = 0; i < 25; i++){
      //random numbers from 0 to 15:
      num = rand() % 15;
      for(int j = 0; j < num; j++) cout << "*";
      cout << endl;
   }

   return 0;
}

The program will simply output rows of stars to the screen 25 times. However, you may not see 25 rows since the inner loop will strictly be less than the generated number. If the random number is 0, the loop will not run and leave a blank row.

One possible output is this:

****
**

*
********
****
**
************
*******

***


*******
****

*********
*****
*****
***

********
*******
****
**

Another possible output is this:

*********
****
******
**
******
***
*********
********
********
****
***
************
**
*******
********
***
*************
*****
*********
***
******
**********
********
******
***

I'm sure you get the picture. Each time you run the program, the output will be different.


<< Tutorial 18
C++ Home >>