Fast pseudorandom number generator for cryptography in C

前端 未结 4 2862
傲寒
傲寒 2021-02-20 16:35

I was using the following code to generate sequence of pseudo-random numbers that was used for cryptographic purposes, but then I read somewhere that it may not be very secure.

4条回答
  •  余生分开走
    2021-02-20 16:56

    I would recommend the Mersenne-Twister, which I have used time and again.

    The C code is here.

    If you use C++11, you have mersenne twister as a part of the library itself. Mersenne Twister is currently one of the best algorithms out there.

    Here is how I would implement in C++11, as a function. It is very straightforward. the mt19937 is th built in Mersenne Twister in C++11.

     std::vector classname::mersennetwister(const int& My,const int& Mz,const int& Ny,const int& Nz)
    {
    int ysize = (My + 2*Ny + 1);
    int zsize = (Mz + 2*Nz + 1);
    int matsize = ysize*zsize;
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::mt19937_64 generator (seed);
    std::uniform_real_distribution distribution(0,1);
    std::vector randarray = f.array1dgen(matsize,0);
    for (int i=0;i

提交回复
热议问题