Case Study: Generating Random Characters in C++

 A character is coded using an integer. Generating a random character is to generate an integer

Computer programs process numeric data and characters. You have seen many examples involving numeric data. It is also important to understand characters and how to process them. This section gives an example of generating random characters.

Every character has a unique ASCII code between 0 and 127. To generate a random char­acter is to generate a random integer between 0 and 127. In Section 3.9, you learned how to generate a random number. Recall that you can use the srand(seed) function to set a seed and use rand() to return a random integer. You can use it to write a simple expression to generate random numbers in any range. For example, and use rand() to return a random integer. You can use it to write a simple expression to
generate random numbers in any range. For example,

In general

So, you can use the following expression to generate a random integer between 0 and 127:

rand() % 128

Now let us consider how to generate a random lowercase letter. The ASCII codes for lower­case letters are consecutive integers starting with the code for ‘a’, then that for ‘b’, ‘c’, . . . , and ‘z’. The code for ‘a’ is

static_cast<int>(‘a’)

So a random integer between static_cast<int>(‘a’) and static_cast<int>(‘z’) is

static_cast<int>(‘a’) +

rand() % (static_cast<int>(‘z’) – static_cast<int>(‘a’) + 1)

Recall that all numeric operators can be applied to the char operands. The char operand is cast into a number if the other operand is a number or a character. Thus, the preceding expres­sion can be simplified as follows:

(‘a’ + rand) % (‘z’ – ‘a’ + 1)

and a random lowercase letter is

static_cast<char>(‘a’ + rand() % (‘z’ – ‘a’ + 1))

To generalize the foregoing discussion, a random character between any two characters ch1 and ch2 with ch1 < ch2 can be generated as follows:

static_cast<char>(ch1 + rand() % (ch2 – chi + 1))

This is a simple but useful discovery. Listing 4.3 gives a program that prompts the user to enter two characters x and y with x <= y and displays a random character between x and y.

Listing 4.3 DisplayRandomCharacter.cpp

1 #include <iostream>

2 #include <cstdlib>

3 using namespace std;

4

5 int main()

6 {

7    cout << “Enter a starting character: “;

8    char startChar;

9    cin >> startChar;

10

11   cout << “Enter an ending character: “;

12   char endChar;

13   cin >> endChar;

14

15   // Get a random character

16   char randomChar = static_cast<char>(startChar + rand() %

17   (endChar – startChar + 1));

18

19   cout << “The random character between ” << startChar << ” and “

20   << endChar << ” is ” << randomChar << endl;

21

22 return 0;

23 }

The program prompts the user to enter a starting character (line 9) and an ending character (line 13). It obtains a random character between these two characters (may include these two characters) in lines 16–17.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *