mardi 24 novembre 2020

how to storing pre-defined random numbers in c++ [closed]

randNum() function internally contains a sequence of pre-defined 10 numbers E.g.) 3,7,2,5,1,2,8,9,4,6

Then the function returns one number a time sequentially. In the above case, the function should return 3 for the first call and 7 next, and so on. The sequence must be rotational. If the function is called 12 times, the value returned must be 7

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<random>

int randNum();
using namespace std;

int main()
{
    int num;
    cout << "Enter the number ";
    cin >> num;

    int rNum[100];

    for (int i = 0; i < num; i++)
    {
        rNum[i] = randNum();
        cout << rNum[i];
    }
}

int randNum()
{
    static default_random_engine engine(static_cast<unsigned int>(time(nullptr)));
    uniform_int_distribution<unsigned int> randomNum(0, 9);

    static int rNum[10];
    int *ptr;

    for (int i = 0; i < 10; i++)
    {
        rNum[i] = randomNum(engine);
        ptr = &rNum[i];
    }
    return *ptr;
}

But this code doesn't contain pre-defined 10 numbers. I don't know how to fix it.




Aucun commentaire:

Enregistrer un commentaire