c++11 boost - 数学(2) crc、random

2018-07-12 22:13:29

crc 循环冗余检验码,它用特定的规则处理数据,计算出一个校验和。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
#include <algorithm>
using namespace std;

#include <boost/crc.hpp>
using namespace boost;

void case1()
{
    crc_32_type crc32;

    cout << hex;
    cout << crc32.checksum() << endl;

    crc32.process_byte('a');
    cout << crc32.checksum() << endl;

	crc32.reset(); //复位crc
    crc32.process_bytes("1234567890", 10);
    cout << crc32.checksum() << endl;

    char szCh[] = "1234567890";
    crc32.reset();

    crc32.process_block(szCh, szCh + 10);
    cout << crc32.checksum() << endl;
}

void case2()
{
    crc_32_type crc32;

    cout << hex;
    crc32.process_bytes("1234567890", 10);
    cout << crc32() << endl; //使用()获取crc值

    string str = "1234567890";
    crc32.reset();

    cout << std::for_each(str.begin(), str.end(), crc32)() << endl;   
    cout << crc32() << endl;

}

int main()
{
    case1();
    case2();
}
[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
0
e8b7be43
261daee5
261daee5
261daee5
261daee5
0


random 库提供了30多个随机数发生器用来生成伪随机数。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
//using namespace std;

#include <boost/random.hpp>
using namespace boost;

void case1()
{
    mt19937 rng(time(0)); //以当前时间为种子创建随机数发生器

    std::cout << mt19937::min() << "<->"
        << mt19937::max() << std::endl;

    for (int i = 0;i < 5;++i)
    {
        std::cout << rng() << ",";
    }
    rng.discard(5);  //丢弃5个随机数
    std::cout << rng() << std::endl;
    
    std::vector<int> vec(10);
    rng.generate(vec.begin(), vec.end());

}

void case2()
{
    mt19937 rng(time(0));
    std::cout <<  rng() << std::endl;

    mt19937 rng2(rng);

    for (int i = 0;i < 10;++i)
    {   assert(rng() == rng2());    }
}

//随机数分布器
void case3()
{
    //using namespace boost::random;
    mt19937 rng(time(0));

	//只生成0-255
    random::uniform_int_distribution<> ui(0, 255);
    for (int i = 0;i < 10;++i)
    {   std::cout << ui(rng) << ","; }
    	
    assert(ui.a() == 0 && ui.b() == 255);
    std::cout << std::endl;

    uniform_01<> u01;
    for (int i = 0;i < 10;++i)
    {   std::cout << u01(rng) << ",";    }
    std::cout << std::endl;

    normal_distribution<> nd(1, 2);  //定义均值为1,方差为2的正太分布
    int count = 0;
    for (int i = 0;i < 10000;++i)
    {
        if (abs(nd(rng) - 1) <= 2.0)
        {   ++count;    }
    }
    std::cout << "normal_distribution:" << 1.0 * count / 10000 << std::endl;
}

void case4()
{
	std::cout << "case4" << std::endl;
    mt19937 rng((int32_t)time(0));
    uniform_smallint<>  us(1,100);

    variate_generator<mt19937&, uniform_smallint<>> gen(rng, us);
    for (int i = 0; i < 10 ; ++i)
    {       std::cout << gen() << std::endl;  }

}

//真随机数生成器
template<typename Rng >
void rand_bytes(unsigned char *buf, int buf_len)
{
    typedef variate_generator<Rng, uniform_smallint<>> var_gen_t;
    static var_gen_t
        gen(Rng((typename Rng::result_type)time(0)),
                uniform_smallint<>(1,255));

    generate_n(buf, buf_len, std::ref(gen));
    //for (int i = 0; i < buf_len; ++i)
    //{   buf[i] = gen();}
}

void case5()
{
    unsigned char buf[10];

    rand_bytes<mt19937>(buf, 10);
    for (int i = 0;i < 10 ;++i)
    {       std::cout << (short)buf[i] << ",";   }
    std::cout << std::endl;

    rand_bytes<rand48>(buf, 10);
    for (int i = 0;i < 10 ;++i)
    {       std::cout << (short)buf[i] << ",";   }

    std::cout << std::endl;
}

//////////////////////////////////////////

#include <boost/nondet_random.hpp>

class boost::random_device::impl
{
    private:
        rand48 rng;
    public:
        impl():rng(time(0))
    {   std::cout << "random_device::impl ctor\n";   }

    ~impl() 
    {   std::cout << "random_device::impl dtor\n";   }

    unsigned int operator()()
    {   return rng();   }
};

boost::random_device::random_device()
: pimpl(new impl)
{}

boost::random_device::~random_device()
{   delete pimpl;}

double boost::random_device::entropy() const
{   return 10;}

unsigned int boost::random_device::operator()()
{   return (*pimpl)();}

void case6()
{
    random_device rng;
    for (int i = 0 ;i < 10 ; ++i)
    {   std::cout << rng() << ",";   }
    std::cout << std::endl;

    uniform_real<> ur(1.0, 2.0);
    for (int i = 0 ;i < 10 ; ++i)
    {   std::cout << ur(rng) << ","; }
    std::cout << std::endl;

    variate_generator<random_device&, uniform_smallint<>> 
        gen(rng, uniform_smallint<>(0,255));
    for (int i = 0 ;i < 10 ; ++i)
    {   std::cout << gen() << ",";   }
    std::cout << std::endl;
}


int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
    case6();
}
[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
0<->4294967295
3113031956,291716705,2522322126,1923520745,2057158534,2209698351
3113031956
185,17,150,114,122,129,253,91,106,11,
0.514485,0.11043,0.104362,0.411267,0.962356,0.362338,0.0086774,0.117555,0.116503,0.0393367,
normal_distribution:0.8635
case4
57
6
27
46
35
41
97
25
54
1
117,21,82,156,95,246,117,55,94,151,
128,22,102,175,133,254,112,128,166,99,
random_device::impl ctor
833916427,1265223321,2062845551,643798674,1157889597,1290953563,977654046,2114468287,1598360055,1003726253,
1.12311,1.3363,1.37297,1.02374,1.4907,1.30694,1.01009,1.36202,1.33151,1.07596,
255,98,30,206,183,182,159,53,176,242,
random_device::impl dtor


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/ad


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。