Showing posts with label cpp_random. Show all posts
Showing posts with label cpp_random. Show all posts

Apr 12, 2016

[random number] sample code

[reddit] random_numbers_generation_in_c
Random numbers generation in C++


 
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
using namespace std;

int main() {
    random_device rd;

    vector<uint32_t> v(mt19937::state_size);
    generate(v.begin(), v.end(), ref(rd));
    seed_seq seed(v.begin(), v.end());
    mt19937 mt(seed);

    vector<uint32_t> v64(mt19937_64::state_size * 2); // because 2 * 32 == 64
    generate(v64.begin(), v64.end(), ref(rd));
    seed_seq seed64(v64.begin(), v64.end());
    mt19937_64 mt64(seed64);

    cout << mt() << endl;
    cout << mt64() << endl;
}

Feb 25, 2014

[c++][session id gen][note] Generate Session Id

#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;                               
}