lundi 26 octobre 2020

C++ how to stop a random engine from producing negative numbers?

I have a function that im trying to produce random numbers between 18 and 5

int randomAge() {
  int random;
  std::random_device rd;
  static std::default_random_engine generator(rd()); //produce a static random engine
  std::normal_distribution<double> distribution(18, 52);  //random age between 18 and 52
  random = distribution(generator);
  return random;
}

However I am recieving negative numbers and even numbers that are higher than 52. How can I fix this so it produces numbers between 18 and 52?




Aucun commentaire:

Enregistrer un commentaire