vendredi 22 mars 2019

Random Number Generation C++

I'm trying to learn from this code I found online, it has two functions that generates a random integer but I'm having a hard time understanding them. Could these just be substituted for a normal random int generation instead? Here's the code, thanks in advance!

namespace {
    std::random_device rd;
    std::mt19937 mt(rd());

    int generateRandNumber(int exclusiveMax) {
        std::uniform_int_distribution<> length(0, exclusiveMax - 1);
        return length(mt);
    }

    int generateRandNumber(int min, int max) { // inclusive min/max
        std::uniform_int_distribution<> length(0, max - min);
        return length(mt) + min;
    }

  bool randTrueFalse() {
    static const int random = static_cast<int>(std::log2(RAND_MAX));
    return (rand() >> random) & 1;
  }
}




Aucun commentaire:

Enregistrer un commentaire