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

#include<iostream>
#include<conio.h>


using namespace std;

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

    }

    cout<< endl;
    return 0;
}

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).

commented: Great +2

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.