How to generate a random string in C++ -


After a few hours of screwing around I can not know how to create a random string generator in C ++ Go. I was wondering if you could show me how you would go about doing this.

I'm not sure enough what you are asking, but it seems that you have these lines Want something with:

  const std :: string VALID_CHARS = "abcdefghijklmnopqrstuvwxyz ABCDFGHIKLMNOPCRSTUVWXYZ 0123456789"; Std :: default_random_engine generator; Std :: uniform_int_distribution & lt; Integer & gt; Distribution (0, VALID_CHARS.size () - 1); Std :: ostringstream oss; (Std :: size_t i = 0; i & lt; SOME_SIZE; ++ i) for (oss & lt; VALID_CHARS [distribution (generator)];} Std :: string your_random_string = oss.str ();  

You can make a little simpler by changing the loop:

  const std :: string VALID_CHARS = "abcdefghijklmnopqrstuvwxyz ABCDFGHIKLMNOPCRSTUVWXYZ 0123456789"; Std :: default_random_engine generator ; Std :: uniform_int_distribution & lt; integer & gt; distribution (0, VALID_CHARS.size () - 1); Std :: string your_random_string; Std :: generate_n (std :: back_inserter (your_random_string), SOME_SIZE, [& amp; ] () {Return VALID_CHARS [distribution (generator)];});  

In addition to this, You can use C ++ 11, so avoid using standard standard library functions ( srand and rand ), new C ++ 11 & Lt; random & gt; Library gives more control over the type and distribution of random number generators for those generators.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -