File tree Expand file tree Collapse file tree 1 file changed +16
-14
lines changed
Expand file tree Collapse file tree 1 file changed +16
-14
lines changed Original file line number Diff line number Diff line change 3939#endif
4040
4141#include < math.h>
42+ #include < random>
4243#include < string>
4344#include < iostream>
4445#include < fstream>
4849// --------------------------------------------------------------
4950inline std::string random_string ( size_t length ){
5051
51- srand (ofGetElapsedTimeMillis ());
52+ static auto & chrs = " 0123456789"
53+ " abcdefghijklmnopqrstuvwxyz"
54+ " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
55+ " !?*+{}[]()=$-_" ;
5256
53- auto randchar = []() -> char
54- {
55- const char charset[] =
56- " 0123456789"
57- " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
58- " abcdefghijklmnopqrstuvwxyz"
59- " !¿?*+{}[]%&()=%$-_" ;
60- const size_t max_index = (sizeof (charset) - 1 );
61- return charset[ rand () % max_index ];
62- };
63- std::string str (length,0 );
64- std::generate_n ( str.begin (), length, randchar );
65- return str;
57+ thread_local static std::mt19937 rg{std::random_device{}()};
58+ thread_local static std::uniform_int_distribution<std::string::size_type> pick (0 , sizeof (chrs) - 2 );
59+
60+ std::string s;
61+
62+ s.reserve (length);
63+
64+ while (length--)
65+ s += chrs[pick (rg)];
66+
67+ return s;
6668}
6769
6870// --------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments