I am trying to generate my key random and joining a few hyphen after the key , but however when I try to do this I got no suitable user-defined conversion from \"std::vect
As already suggested in comments, the second argument of randomString
is std::string
but you are passing std::vector
to it.
Thus you get the current compilation error.
For instance, if the type of the first argument of join
is std::vector
, the caller side of randomString
should be as follows:
std::vector t{
randomString(15, reverse.ascii_lowercase()),
randomString(15, reverse.ascii_uppercase()),
randomString( 5, reverse.digits()),
randomString( 5, reverse.punctuation()) };
std::cout << reverse.join(t, "--") << std::endl;
Next,
how can I include srand(time(NULL)); instead of declaring them every time on main
In C++, the life time of static local variables starts when the function is called and ends when the program ends.
Therefore, srand(time(NULL))
in the following setSeed
is called only once through the run-time and in this way you can include srand(time(NULL))
in the randomString
instead of declaring them in the main function.
(Please note that srand
is in effect globally and if you are calling rand()
in other functions with different purposes then you also need to call this setSeed
in these functions. Then, in this case, the main function seems to be the most appropriate point to call srand
.)
Live Demo
void setSeed()
{
static bool set = false;
if(!set)
{
srand(time(NULL));
set = true;
}
}
std::string randomString(uint length, std::string string)
{
setSeed(); // I added this line.
std::vector indexesOfRandomChars(length); // array of random values that will be used to iterate through random indexes of 'charIndex'
for (uint i = 0; i < length; ++i) // assigns a random number to each index of "indexesOfRandomChars"
indexesOfRandomChars[i] = rand() % string.length();
std::string key = ""; // random string that will be returned by this function
for (uint i = 0; i < length; ++i)// appends a random amount of random characters to "randomString"
{
key += string[indexesOfRandomChars[i]];
}
return key;
}
BTW, although rand()
should be usually implemented using something better LCG,
but, for instance as noted in C++ standard draft n4687, the algorithm used in rand()
is completely compiler implementation defined:
29.6.9 Low-quality random number generation [c.math.rand]
int rand(); void srand(unsigned int seed);
... rand’s underlying algorithm is unspecified. Use of rand therefore continues to be non-portable, with unpredictable and oft-questionable quality and performance.
Fortunately, in C++11 and over, we can use
to generate a guaranteed quality randomness.
Thus I recommend you to use them as follows.
Here I also avoid recursive construction of std::minstd_rand
and make the function thread-safe applying the accepted answer in this post.
If you need more high-quality randomness, you can use std::mt19937
instead of std::minstd_rand
:
Live Demo
#include
std::string randomString(
std::size_t length,
const std::string& str)
{
static thread_local std::minstd_rand gen(std::random_device{}());
std::uniform_int_distribution dist(0, str.length()-1);
std::string ret;
ret.reserve(length);
for(std::size_t i = 0; i < length; ++i){
ret.push_back(str[dist(gen)]);
}
return ret;
}