mardi 17 juillet 2018

Why i get the same random numbers every time i run my program? [duplicate]

This question already has an answer here:

so basically i am trying to generate random numbers (1 to 6), but every time i run my program i always get the same two number. here is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// function prototype
int random();

// main function
int main()
{
    int rand1=random();
    int rand2=random();
    cout << rand1 << endl;
    cout << rand2 << endl;

    cin.get();
    return 0;
}

//function definition
int random()
{
    const int MAX_NUMBER = 6;
    const int MIN_NUMBER = 1;
    unsigned seed=time(0);
    srand(seed);

    int randomNumber;
    randomNumber=(rand() % (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER;


    return randomNumber;
}

I'm not sure what is wrong and why i always get the same two random numbers.




Aucun commentaire:

Enregistrer un commentaire