samedi 22 juillet 2023

C++ mt19937 getting the same sequence multiple times

im trying to create a deterministic hash generator using a pseudo random number sequence with the mt19937 engine in the random library. Therefore i need to get the same sequence of numbers everytime i feed the engine the same seed. My code looks like this:

#include <iostream>
#include <random>
#include <time.h>
#include <Windows.h>

int randomnumber = 0;

int main(){
    std::random_device rd;
    std::uniform_int_distribution<int> dist(0, 1);

    std::mt19937(123);
    for (int i = 0; i < 32; i++) {
        randomnumber |= ((unsigned)(dist(rd)) << i);
    }
    std::cout << (unsigned int)randomnumber << std::endl;

    std::mt19937(112);
    for (int i = 0; i < 32; i++) {
        randomnumber |= ((unsigned)(dist(rd)) << i);
    }
    std::cout << (unsigned int)randomnumber << std::endl;

    std::mt19937(123);
    for (int i = 0; i < 32; i++) {
        randomnumber |= ((unsigned)(dist(rd)) << i);
    }
    std::cout << (unsigned int)randomnumber << std::endl;

    return 0;
}

I tried setting the seed everytime i generated a new hash but the random number sequence was different never the less.

The outputs are

1126954929

3745251263

3753639871




Aucun commentaire:

Enregistrer un commentaire