jeudi 1 avril 2021

Side-effect of std::srand/rand and libuuid uuid_generate

For testing I need reproducible random numbers. The randomness isn't that important. Currently I'm setting a seed std::srand(x) and use std::rand() to generate. As expected the numbers are always the same on multiple runs with the same seed.

For some other parts I need UUIDs. I use the utils-linux libuuuid.

But running this code:

  std::srand(2);

  uuid_t uuid;
  
  int x = std::rand();
  uuid_generate(uuid);
  int y = std::rand();

y is different on every run.

I already tried to use uuid_generate_time,uuid_generate_time_safe but this doesn't generate fully random UUIDs on multiple calls:

d4e25820-92ca-11eb-9b39-f91e898522ad
d4e25821-92ca-11eb-9b39-f91e898522ad
d4e25822-92ca-11eb-9b39-f91e898522ad
d4e25823-92ca-11eb-9b39-f91e898522ad

Is there any other way I could implement this?




Aucun commentaire:

Enregistrer un commentaire