Create Random Alpha-Numeric String Using C++



Alpha-numeric string are the strings that containing both alphabets and numbers mixed together. These are generally used security system for generating passwords or hash keys. In this article, we will learn all the approaches for developing a C++ program to generate random alpha numeric stiring. First of all, let's understand the problem statement,

We have to input a positive integer for the length of string. The program should output a string of the size containing random characters and numbers. For example,

// Input Number : Length of alpha-numeric string
5

// Output String : Alpha-numeric string
fd23j

Approaches to Create Random Alpha Numeric String in C++

Here are the list of approaches to create random alpha-numeric string in c++, which we will be discussing in this article with stepwise explanation and complete example codes.

Using rand() Function to Create Random Alpha-numeric String

The rand() function is used to generete random number in a specified range. We will use this function to guess an random index from a array of alpha-numeric characters. And then we repeat this process to make the specified number of characters. Let's see an example of this process.

Example

In the code below, we declared a function to generate random number. In the function we have a charset array containing all the alpha numeric characters, then we use rand() function to select random index from array for each character of alpha-numeric string.

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

string generateRandomString(int length) {
    const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int maxIndex = sizeof(charset) - 2; // -1 for '\0', another -1 to get index

    string result;
    srand(time(0)); // seed the random generator
    for (int i = 0; i < length; ++i)
        result += charset[rand() % maxIndex];

    return result;
}

int main() {
    cout << generateRandomString(12) << endl;
}

The output of the above code will be:

1f7mgk6b4rfi

Using generete() Function to Create Random Alpha-numeric String

The c++ generete() function is used to fill a range by repeatedly calling a generetor function or lambda for each element. We will use each character genereted by the lamda to select random index from charset using uniform distribution. Now, let's see an example for this.

Example

In the code below, we used generete function to select random index from the charset array.

#include <iostream>
#include <algorithm>
#include <random>
#include <string>
using namespace std;

string generateRandomString(int length) {
    const string charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> dis(0, charset.size() - 1);

    string result(length, ' ');
    generate(result.begin(), result.end(), [&]() {
        return charset[dis(gen)];
    });

    return result;
}

int main() {
    cout << generateRandomString(12) << endl;
}

The output of the above code will be:

awd2f5h7em6w
Updated on: 2025-04-22T11:41:26+05:30

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements