Answers for "why does a string have random characters in c++"

C++
2

getting a random letter in c++

char c;
    int r;

    srand (time(NULL));    // initialize the random number generator
    for (i=0; i<num; i++)
    {    r = rand() % 26;   // generate a random number
          c = 'a' + r;            // Convert to a character from a-z
          cout << c;
    }
Posted by: Guest on June-24-2020
0

generate random string in c++

#include <random>
#include <string>

std::string random_string()
{
     std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

     std::random_device rd;
     std::mt19937 generator(rd());

     std::shuffle(str.begin(), str.end(), generator);

     return str.substr(0, 32);    // assumes 32 < number of characters in str         
}
Posted by: Guest on September-01-2021

Code answers related to "why does a string have random characters in c++"

Browse Popular Code Answers by Language