#include <vector> #include <random> #include <limits> #include <algorithm> #include <iostream> class RandomGenerator { private: using Uniform_Dist_ushort_t = std::uniform_int_distribution<unsigned short>; public: unsigned long long GenSessionId() { std::vector<unsigned int> argv{ ushort_dist(mt), ushort_dist(mt), ushort_dist(mt), ushort_dist(mt)}; std::shuffle(argv.begin(), argv.end(), mt); sessionID_u seid; seid.a = argv[0]; seid.b = argv[1]; seid.c = argv[2]; seid.d = argv[3]; return seid.id; } private: union sessionID_u { struct{ unsigned short a; unsigned short b; unsigned short c; unsigned short d; }; unsigned long long id; }; sessionID_u seid; private: std::random_device rd; std::mt19937 mt{rd()}; Uniform_Dist_ushort_t ushort_dist{0, std::numeric_limits<unsigned short>::max()}; }; int main() { std::cout << RandomGenerator().GenSessionId() << std::endl; }
Feb 25, 2014
[c++][session id gen][note] Generate Session Id
Labels:
cpp,
cpp_note,
cpp_original,
cpp_random,
cpp_trick,
cpp11
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.