Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Generate a random hexidecimal string of large number of nibbles.
- * (Created because needed a 204-bit integer.)
- *
- * davep 17-Mar-2015
- */
- #include <iostream>
- #include <string>
- #include <random>
- #include <algorithm>
- #include <chrono>
- using namespace std;
- /* With assistance from
- * https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c
- * http://www.cplusplus.com/reference/random/linear_congruential_engine/linear_congruential_engine/
- */
- string random_hex_string( size_t length )
- {
- unsigned seed1 = chrono::system_clock::now().time_since_epoch().count();
- string charset { "0123456789abcdef" };
- auto gen = bind(uniform_int_distribution<unsigned int>{0,charset.size()-1},
- default_random_engine{seed1});
- auto randchar = [&charset,&gen]() -> char
- {
- return charset.at(gen());
- };
- string str(length,0);
- generate_n( str.begin(), str.size(), randchar );
- return str;
- }
- int main()
- {
- string str = random_hex_string(51);
- cout << str << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement