mercredi 24 février 2016

C++ default_random_engine

My program is to produce random two digit numbers. The issue I am having is that when I call multiple instances I get the same number sequence. However, each instance must be unique. How do I accomplish this?

#include "rowarray.h"
#include <iostream>
#include <random>

RowArray::RowArray(const int size)
{
     validate(size);//to check if listSize is negative

     listSize = size;
     list = new int[size];

     std::default_random_engine engine;
     std::uniform_int_distribution<int> dist(10, 99);

     for(int index = 0; index < size; index++)
     {
          list[index] = dist(engine);
     }
}

RowArray::~RowArray()
{
    delete [] list;
}

void RowArray::validate(const int index)
{
    if (index < 0 || index >= listSize)
    {
        std::cout << "ERROR: INVALID SUBSCRIPT";
        exit(EXIT_FAILURE);
    }
}

int RowArray::getSize()
{
    return listSize;
}

int RowArray::getValue(int index)
{
    validate(index);
    return list[index];
}

RowArray.h

#ifndef ROWARRAY_H
#define ROWARRAY_H



class RowArray
{
public:

    RowArray(const int size);
    ~RowArray();

    int getValue(const int index);
    int getSize();

private:

    void validate(const int index);//validate subscripts

    int *list;//pointer to array
    int listSize;//number of elements of array
};

#endif // ROWARRAY_H




Aucun commentaire:

Enregistrer un commentaire