mardi 7 juin 2016

Same random number generates

I am new to c++, and i am writing a program for generating random phone number. But the program is giving same random number. I have also used

srand (time (null));

and

srand (time (0));

My code is as follows

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int randomno (int array [], int size)
{

    array [0]=rand()%3+7;
    for (int i=1; i<size; i++)
    {
        array [i]=rand()%10;
    }
    int phoneno=array[size];
    return phoneno;
}
int main ()
{
    srand (time (NULL));
    int no[10];
    cout << "Random phone no is " << randomno (no, 10);
}

This program should show numbers in the format (9 or 8 or 7 then 9 more digits). The program below runs well and will give you an idea what i want to do.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
    srand (time (NULL));
    cout << "The Random phone No. is ";
    int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
    d1=rand()%3+7;
    d2=rand()%10;
    d3=rand()%10;
    d4=rand()%10;
    d5=rand()%10;
    d6=rand()%10;
    d7=rand()%10;
    d8=rand()%10;
    d9=rand()%10;
    d10=rand()%10;
    cout << d1<<d2<<d3<<d4<<d5<<d6<<d7<<d8<<d9<<d10;
}

But later i decided to use Arrays and functions.

Help me out.




Aucun commentaire:

Enregistrer un commentaire