Generate Random String of Characters

Posts 1–9 of 9 · Page 1 of 1
Generate Random String of Characters
I am trying to generate a random string of characters and output them on the screen, I'm using a console application. The only rules are it has to be exactly 6 characters, no symbols or special characters, and there can't be a pattern. Any ideas? Below is an example of what it would output:

HsxnJs
Im on an android so heres psuedo code
Char szString[6];
For(i = 6; i != 6, i++)
Szstring[i] = (char)rand(google char numeric values)
Quote Originally Posted by kibbles18 View Post
For(i = 6; i != 6, i++)
I lol'ed.
I would do it kibbles way too though, it is simple and easy.
Code:
#include <iostream> 
#include <string> //Needed for strings.
#include <time.h> //Needed for time function.
using namespace std;
int main()
{
		string CharacterSet = "lmntuvwxyzABCDEFGghijHIJKLMNOPopqrsQRSTUVWXabcdefkYZ"; //Characters you want to include in the final string. I broke the sequence for better randomization.
		string FinalString; //Will store our random characters.
		srand (time(NULL)); //Initialize a random seed for randomizer based on the time.
		for (int i = 0; i < 6; ++i) //Need 6 characters, so loop 6 times.
		{ 
        FinalString += CharacterSet[rand() % (CharacterSet.length())]; //Select a random character from the CharacterSet.
		}
		cout << FinalString.c_str() << endl << endl; //Display the string by converting it into c style string.
		system("PAUSE"); //Pause this shit.
}
Just expanding Hassan's a little bit:

Code:
char *random_string(int len)
{
     char *alphabet = "lmntuvwxyzABCDEFGghijHIJKLMNOPopqrsQRSTUVWXabcdefkYZ";
     int   alphaLen = (int)strlen(alphabet);
     char *random = (char*)calloc(1, len + 1);  
     for(int i = 0; i < len; i++)
         random[i] = alphabet[rand() % alphaLen];
     return random;
}
Remember to call srand ONCE before your first call to random_string. You only need to seed the random once. So for example:
Code:
srand(time(NULL));
char *random1 = random_string(10);
char *random2 = random_string(10); //note, no further calls to srand.
free(random1);
free(random2); //free calloc'd memory.
Yes, this is remnants of C, get off me.
^ Good work nig, but I hate freeing up memory manually.
Switch calloc to "new char[len + 1]"

and before the return statement put "random[len] = '\0';"

So:

Code:
char *random_string(int len)
{
     char *alphabet = "lmntuvwxyzABCDEFGghijHIJKLMNOPopqrsQRSTUVWXabcdefkYZ";
     int   alphaLen = (int)strlen(alphabet);
     char *random = new char[len + 1];
     for(int i = 0; i < len; i++)
         random[i] = alphabet[rand() % alphaLen];
     random[len] = '\0';
     return random;
}
Using new will still require you to manually free memory. Instead, a smart pointer such as shared_ptr should be used.
Quote Originally Posted by Fovea View Post
Using new will still require you to manually free memory. Instead, a smart pointer such as shared_ptr should be used.
Never heard of it. Mind telling how it works ?
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us