Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random String Generation

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

char *charStr;
int stringLength;

void genRandom() {
    static const char alphanum[] =
        "0123456789"
        "!@#$%^&*"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < stringLength; ++i) {
        charStr[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    charStr[stringLength] = 0;
}

int main()
{
    while(true)
    {
        genRandom();
        cout < charStr;
    }
    return 0;

}

The issue comes in when you compile it. It will compile just fine but nothing displays and then the program will cease to run. So my question is, what is wrong with this code?

like image 356
Cistoran Avatar asked Feb 19 '11 20:02

Cistoran


People also ask

What is a random string generator?

The random string generator creates a series of numbers and letters that have no pattern. These can be helpful for creating security codes. With this utility you generate a 16 character output based on your input of numbers and upper and lower case letters. Random strings can be unique.

What is a random string?

A random string represents a series of alphanumeric characters that have no particular pattern. Although there is no absolute random string because their generation uses mathematical logic, random strings can be unique.

Can we generate random string?

Using Java 8 StreamYou can also generate a random alphanumeric string of fixed length using streams in Java. A random string is generated by first generating a stream of random numbers of ASCII values for 0-9, a-z and A-Z characters.

How do you make a string unique?

If you simply want to generate a unique string and it does not have to be cryptographically secure, then consider using the uniqid() function. It returns a unique identifier based on the current timestamp. echo uniqid( 'user_' , true);


1 Answers

Several issues with your code:

cout < charStr;

should be:

cout << charStr;

If you compile with the g++ -Wall argument (warning all), that error becomes easily apparent.

Also, you never set the value of stringLength! This is one example of why you generally should not use global variables--it can be hard to keep track of them. The unset value of stringLength could do weird things depending on your compiler--many compilers will simply initialize the value to 0, but some will set it to a random value. This undefined behavior can cause major headaches, so be very careful about that, and try to always initialize your variables when appropriate (this is generally a bigger issue with pointers, but the problem can still remain for other variables).

A fixed program is below:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    while(true)
    {
        cout << genRandom();
    }
    return 0;

}

Still using global variables, but I think this is a bit more of an appropriate use of them. I'm not sure what you were trying to accomplish by having the global char* string, just a headache waiting to happen and not really giving any advantages in your code. Generally in C++ it's better to use the C++ standard library string when you can--though in this case, your code really didn't need strings.

like image 69
DashRantic Avatar answered Oct 06 '22 18:10

DashRantic