samedi 12 mars 2022

How to generate random number but different every time when we run program? [duplicate]

I want to generate random number for ONE TIME PASSWORD(OTP)... But every time I run the program it gives me same OTP as it gave me last time... I want to generate random numbers but numbers should be different every time when I run the program... Please help!!

Here is my code:

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    int otp[4]={0,0,0,0};
    //loop to generate the otp
    for (int i=0;i<4;i++)
    {
        otp[i]=1+rand()%10;
    }
    cout<<"Your OTP is: ";
    //loop to print the otp on screen
    for(int i=0;i<4;i++)
    {
        cout<<otp[i];
    }
    cout<<endl;
    return 0;
}

When I run this program for first time it gave me OTP as 2851 and second time it gave me same OTP....




Aucun commentaire:

Enregistrer un commentaire