0% found this document useful (0 votes)
12 views3 pages

C++ Random Character Generator Code

The document provides a C++ solution for generating random characters of different types, including small letters, capital letters, special characters, and digits. It includes a function to generate a random number within a specified range and a main function that outputs one random character of each type. The random number generator is seeded using the current time to ensure varied outputs.

Uploaded by

soumaaloui18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

C++ Random Character Generator Code

The document provides a C++ solution for generating random characters of different types, including small letters, capital letters, special characters, and digits. It includes a function to generate a random number within a specified range and a main function that outputs one random character of each type. The random number generator is seeded using the current time to ensure varied outputs.

Uploaded by

soumaaloui18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem # 19/2 Solution Using C++

#include <iostream>
#include <string>

using namespace std;

int RandomNumber(int From, int To)


{
//Function to generate a random number
int randNum = rand() % (To - From + 1) + From;
return randNum;
}

enum enCharType { SamallLetter = 1, CapitalLetter = 2,


SpecialCharacter = 3, Digit = 4 };

char GetRandomCharacter(enCharType CharType)


{
switch (CharType)
{

case enCharType::SamallLetter:
{
return char(RandomNumber(97, 122));
break;
}
case enCharType::CapitalLetter:
{
return char(RandomNumber(65, 90));
break;
}
case enCharType::SpecialCharacter:
{
return char(RandomNumber(33, 47));
break;
}
case enCharType::Digit:
{
return char(RandomNumber(48, 57));
break;
}

}
}

[Link]
© Copyright 2022
Problem # 19/2 Solution Using C++

int main()

{
//Seeds the random number generator in C++, called only once
srand((unsigned)time(NULL));

cout << GetRandomCharacter(enCharType::SamallLetter) << endl;


cout << GetRandomCharacter(enCharType::CapitalLetter) << endl;
cout << GetRandomCharacter(enCharType::SpecialCharacter) <<
endl;
cout << GetRandomCharacter(enCharType::Digit) << endl;

return 0;

[Link]
© Copyright 2022

You might also like