lundi 11 juillet 2016

Succesive random numbers in a range

I have this following code:

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

int getRandomNumber(int min,int max)
{
    static const double fraction = 1.0/(static_cast<double>(RAND_MAX)+1.0);
    return static_cast<int>(rand()*fraction*(max-min+1)+min);
}

int main()
{
    srand(static_cast<int>(time(0)));
    std::cout<<getRandomNumber(1,6);
    return 0;
}

If I run this program in succesions, then i get the same number. But when i run this one with cout statement as:

std::cout<<getRandomNumber(1,6)<<getRandomNumber(1,6);

I get different numbers every time. So how is this possible?? Am I missing something?




Aucun commentaire:

Enregistrer un commentaire