Randomization character in a string

I am trying to take the string randomID shuffle it around 26 times then pluck 5 char’s from it then store them into the char array ID. Then convert the char’s back into a string. I am having an issue trying to figure it out. Any advice as to where I am going wrong and how to fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 std::string ACD::getID()
{
	int i;
	const int LENGTH = 5;
	srand(NULL);
	std::string randomID = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
	char ID[LENGTH] = "";
	for (i = 0; i < LENGTH; i++)
	{
		//issue here is at the + 
		int ranID = (int) ((rand() % 26)+randomID);
		//here is with the .at function
		ID[i] = randomID.at(ID);
	}
	return std::string(ID);
}
Last edited on
((rand() % 26)+randomID);

On the left, a number. On the right, a string. What's meant to happen what you add a number and a string?

I think maybe you're trying to pick a random character from inside the string.
char randomCharacter = randomID.at(rand() % 26);



randomID.at(ID);

ID is a char-pointer... what's meant to happen here? I have no idea the logic behind this is. I suspect you're trying to append the chosen character to ID.

Just make ID a string. It's what they're for.

1
2
3
4
5
6
7
8
9
10
11
12
 std::string ACD::getID()
{
srand(NULL); // this will give you always the same "random" values
	std::string randomID = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
	std::string ID;
	for (int i = 0; i < 5; i++)
	{
		char randomCharacter = randomID.at(rand() % 26); // Why 26? There are more than 26 characters to pick from.
		ID += randomCharacter;
	}
	return ID;
}




Last edited on
Topic archived. No new replies allowed.