random alphanumeric string generator for c++



actuially i have been using srand and rand to generate random alphanumeric number but didn't get the desired output some strings were repeated at a regular time whihc is not what i want i want a totally unique string at every output of the program.Please do share a sample code which give the accurate solution to this problem.reference to a library may also help me a lot :) actually what i want to generate unique random alphanumeric string at every output of the program basically this random string wil be used as client id in mqtt CONNECT packet.hope i can explain the scenario now.

EDIT:: The sample code is presented below

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

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

int stringLength = sizeof(alphanum) - 1;
char genRandom()
{
return alphanum[rand() % stringLength];
}
int main()
{
srand(time(0));
std::string Str;
for(unsigned int i = 0; i < 20; ++i)
{
Str += genRandom();
}
cout << Str << endl;

}

the output is repeated at a certain inetrval like:- xvtW45789045dfg srfet7854635fds xvtW45789045dfg

this is the output i get when i run it at every instance i don't want this repetition. At avery instance of running the output i must get a unique string.
rand() and srand() are specifically addressed by the standard, which actually says something to the effect that the generated numbers are of dubious quality, and that you better not use those. Further, time(NULL) isn't necessarily going to vary quickly enough if you're running the program in any sort of a loop (e.g., from the shell), or if you're otherwise reseeding the generator in your program.

Also, using the system time as a seed is prone to error -- what if you adjust the clock?

Does the code you posted exhibit the problem you described?

Higher-order programming fits very elegantly with the random number library -- I tend to write lambdas for this kind of problem.

Here is an example of one way to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <random>
# include <iostream>

int main() {
  constexpr char chset[] =
    "0123456789!@#$%^&*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  auto r_char = [=, r_gen{std::mt19937{std::random_device{}()}}] () mutable {
    return chset[std::uniform_int_distribution<>{0, sizeof chset - 1}(r_gen)];
  };

  for (auto i = 0; i < 20; ++i)
    std::cout << r_char();
  std::cout << "\n";
}
Last edited on
thanks man.Actually yesterday i used the same thing but woth boost library .I dont know whether it is efficient or not but yes its generating unique random strings for every client id and that's what i wanted.SO,could you please tell me which one would be better the normal random generator that the one posted above or the one with boost library with respect to the performance stuff.
Thanks,
Kushal

Edit:-
To answer your above question yes the random string ws repeating after regular intervals just like the output i posted above so srand() and rand() functions dont seem to be useful in this scenario
Last edited on
Topic archived. No new replies allowed.