zoukankan      html  css  js  c++  java
  • 使用boost库生成 随机数 随机字符串

     
    
    #include <iostream>
    #include <boost/random/random_device.hpp>
    #include "boost/random.hpp"
    #include "boost/generator_iterator.hpp"
    using namespace std;
    
    int randString() 
    {
        /*<< We first define the characters that we're going
             to allow.  This is pretty much just the characters
             on a standard keyboard.
        >>*/
        std::string chars(
            "abcdefghijklmnopqrstuvwxyz"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "1234567890"
            "!@#$%^&*()"
            "`~-_=+[{]}\|;:'",<.>/? ");
        /*<< We use __random_device as a source of entropy, since we want
             passwords that are not predictable.
        >>*/
        boost::random::random_device rng;
        /*<< Finally we select 8 random characters from the
             string and print them to cout.
        >>*/
        boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
        for(int i = 0; i < 8; ++i) {
            std::cout << chars[index_dist(rng)];
        }
        std::cout << std::endl;
    }
    int main() 
    {
        cout<<"===============rand String==============="<<endl;
        randString();
    
    
        cout<<"===============rand number==============="<<endl;
          typedef boost::mt19937 RNGType;
          RNGType rng;
          rng.seed(time(NULL));
          boost::uniform_int<> one_to_six( 99999, 999999 );
          boost::variate_generator< RNGType, boost::uniform_int<> >
                        dice(rng, one_to_six);
          for ( int i = 0; i < 6; i++ ) 
          {
              int n  = dice();
              cout << n << endl;
         }
    }                     

     使用boost库生成随机字符串 随机数,生成随机数还是比较方便,随机字符串倒没觉得比rand方便多少,反而麻烦
  • 相关阅读:
    第03组 Beta冲刺(2/5)
    2019 SDN上机第6次作业
    第03组 Beta冲刺(1/5)
    2019 SDN上机第5次作业
    SDN课程阅读作业(2)
    2019 SDN上机第4次作业
    第05组 团队Git现场编程实战
    第05组 团队项目-需求分析报告
    团队项目-选题报告
    第07组 Alpha事后诸葛亮
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9410054.html
Copyright © 2011-2022 走看看