mardi 15 mars 2016

Generate random numbers where each has to be bigger than previous one?

Quick question, I need to write a simple "generator" in c++. That will generate random numbers starting from X adding a random number from range (Y-Z) the tricky part is that each next number has to be bigger than the previous one.

Here is the code I've written.

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

int numer, numer2;
string tab = "  ";
int getrand(int min, int max);

int main()
{
    cout << "First Number : "; cin >> numer;
    cout << "Second Number : "; cin >> numer2;

    fstream plik;
    plik.open("item_list.txt", ios::out | ios::app);

    while (numer <= numer2)
    {
        plik << endl << numer << tab 
             << 2029047116 + getrand(250000, 900000) << endl;
        numer++;
    }

    return 0;
}

int getrand(int min,int max){
     return(rand()%(max-min)+min);
}

I just can't think of any logical way how to get the tricky part done.




Aucun commentaire:

Enregistrer un commentaire