hey guys can anyone tell me what is actually happening in the code given below.??


using namespace std;

int main()
    int randNum = (rand() % 100) + 1;
    while (randNum != 100){
    cout << randNum << ",";
    randNum = (rand() % 100) + 1;


    cout<< endl;
    return 0;
3 Years
Discussion Span
Last Post by basit_3

Generate a random number from 1 to 100.

Output that number, generate another one. Repeat until the generated number is 100.

This is pretty bad code. Firstly, it's including conio.h, which is non-standard and from thirty years ago. It's not actually using anything from conio, so whoever put that there doesn't even know why they put it there, which is a bad sign.

It's also not seeding the "random" number generator, so you'll get the same "random" sequence every time.

The rand() function comes with #include <cstdlib> which whoever wrote this has not done. If it works, that's just chance and not portable; it should have the proper header.

It's also using a very bad random number generator. It's so bad that it basically should never be used, even in learning (because learning to use something this bad just makes no sense).

Edited by Moschops

Votes + Comments

Moschops said.
i think also that using <conio.h> will not be fear, instead you can use <cstdlib>


As moschops said the above code generate random number while the generated number is not equall to 100

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.