How to Create a Random Alpha-Numeric String in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Creating a random alpha-numeric string in C++ means generating random characters from the set of alphanumeric characters (i.e., ‘A’-‘Z’, ‘a’-‘z’, and ‘0’-‘9’) and appending them to a string. In this article, we will learn how to create a random alpha-numeric string in C++. Example Output:a8shg1laCreate a Random Alpha-Numeric String in C++To generate a random alpha-numeric string in C++, we use a random number generator along with the string that contains all the characters and numbers. We can pick a random index for each character in the alphanumeric string of the given size. ApproachDefine a constant string CHARACTERS that contains all the possible characters that can be included in the random string.Create a random number generator using the random_device and mt19937 classes from the <random> library.Create a uniform integer distribution that ranges from 0 to the size of CHARACTERS minus 1.Initialize an empty string random_string. Then, in a loop that runs length times, append a randomly selected character from CHARACTERS to random_string.Return random_string.C++ Program to Create a Random Alpha-Numeric StringThe following program illustrates how to create a random alpha-numeric string in C++. C++ // C++ Program to illustrate how to create a random // alpha-numeric string #include <iostream> #include <random> #include <string> using namespace std; string generateRandomString(int length) { // Define the list of possible characters const string CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv" "wxyz0123456789"; // Create a random number generator random_device rd; mt19937 generator(rd()); // Create a distribution to uniformly select from all // characters uniform_int_distribution<> distribution( 0, CHARACTERS.size() - 1); // Generate the random string string random_string; for (int i = 0; i < length; ++i) { random_string += CHARACTERS[distribution(generator)]; } return random_string; } int main() { int length = 10; string random_string = generateRandomString(length); cout << "Random String: " << random_string << endl; return 0; } OutputRandom String: ihoSqnkXRz Time Complexity: O(N), here N is the length of the string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Create a Random Alpha-Numeric String in C++? B bytebarde55 Follow Improve Article Tags : C++ Programs C++ cpp-strings cpp-random CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads How to Generate Random Number in Range in C++? In C++, we have a <random> header that consists of standard library facilities for random number generation. In this article, we will learn how to generate a random number in range in C++. Example: Input: Range: 1 to 20Output: Random number between 1 and 20 is: 18Generating a Random Number in 2 min read How to Seed a Random Number Generator in C++? In C++, seeding a random number generator is important for generating different sequences of random numbers on each program run. This process consists of initializing the generator with a starting value, known as a seed. This ensures the randomness and unpredictability required for various applicati 2 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read Generate a random Binary String of length N Given a positive integer N, the task is to generate a random binary string of length N. Examples: Input: N = 7Output: 1000001 Input: N = 5Output: 01001 Approach: The given problem can be solved by using the rand() function that generates a random number over the range [0, RAND_MAX] and with the help 5 min read How to generate a vector with random values in C++? Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.The recommended method to initialize a vector with random values is by using 3 min read Like