-7

I am want to make my program to choose a random number

 #include <iostream>
   using namespace std;
   int main(){
   int x=100 y=40;
   while (true)
     {if(x>y)
       {x--;}
     else
       {break;}}}

1 Answers1

1

Unless your compiler doesn't support C++11, use the <random> library. You need to choose an engine, a seed for the engine, and a distribution; if you don't know how to choose, and if you want a random integer, this should do:

#include <random>
#include <iostream>

int main() {
    auto engine = std::mt19937(std::random_device()());
    auto distribution = std::uniform_int_distribution(0, 10); // between 0 and 10 inclusive
    auto random_integer = distribution(engine);
    std::cout << random_integer;
}
papagaga
  • 1,108
  • 8
  • 14