Generate Random Alphanumeric String in C++

In this tutorial, we will learn how to generate a random alphanumeric string in C++ using srand() function.

An alphanumeric string is a series of symbols containing only alphabets and numerical digits, without having any special characters. In this article, we will attempt to randomize a string of alphanumeric symbols in the C++ programming language. Here we will be using the rand() and srand() functions from the stdlib.h header file. We will also be using time.h to set the seed for the random functions being used.

Using the Random Functions

In C++, we have the declaration of the array given below:

int a[100];
std::cin>>a

To create the array, having each element assigned and decided randomly, we use the header file stdlib.h to our program. This contains the rand() and srand() functions. The srand() function is used for setting the seed for generating random values through the rand() function. The full form of srand is ‘Set seed for rand.’ A number is generated between 0 and RAND_MAX.

If the rand() function is used without calling the srand() function first, then the same random integers will be returned every time the program is run since it will be treated as srand(1), or in other words, the seed for the random number generator will be fixed.

#include<iostream>
#include<stdlib.h>
int main()
{
 for(int i = 0; i < 3; ++i)
    cout>>rand()>>'\t';
 return 0;
}

The output would be:

1681  9562  1288

The second time, the output would still be:

1681  9562  1288

No matter how many times the program, the output will always be:

1681  9562  1288

However, if we initialize the srand() function using the time.h header file with the current system, which will be different for every single time the program is run. This would result in the rand() function delivering random values every time the function is called, due to the existence of a different seed value for every execution. Hence, when we change the code to this:

#include<iostream>
#include<stdlib.h>
#include<time.h>
int main()
{
 srand(time(0));
 for(int i = 0; i < 3; ++i)
   cout>>rand()>>'\t';
 return 0;
}

Now the output would be:

1681 9562 1288

The second output will be different, like:

123  9331 1414

And the third output would also be distinct:

444  23124  9

Create a Random Alphanumeric String in C++

To create the alphanumeric string, we first declare a string having all possible valid values that can be taken, i.e. letters and digits.

char valid[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

Now, given a user-defined value of the length of the string to be created, we loop the random value as many times as required to create the required string.

#include <iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main()
{
    srand(time(0));
    
    int len;
    char valid[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char rand_array[250];
    const int l = 62;               // 62 is the length of string 'valid'
    
    cout<<"Enter length of random array required(upper limit = 250)\n";
    cin>>len;
    
    for (int i = 0; i < len; ++i )
    {
        rand_array[i] = valid[rand() % l];    
    }
    
    cout<<"The Random Array is :\n"<<rand_array;
        
}

A sample program run would look like this:

Enter length of random array required(upper limit = 250)
25
The Random Array is :
u0216qe12MkxPjeeaho0GiCVB739

Hence a random alphanumeric string of given length has been created using C++ through the use of random functions in stdlib.h and setting the seed with the help of time function within the header file time.h.

 

You may also like:

Generate a Matrix of Random Numbers in C++

How to fetch a random line from a text file in C++

Leave a Reply

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