jeudi 23 avril 2015

My random number generator is printing duplicate numbers

I'm working on a program that uses a function that would generate 2 random integers that I would later use to ask the user to add and figure out the sum. For the time being, I came across a problem in which it's duplicating the same number.

Here's the program thus far:

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


/*
This function generates a random integer
between 0 and max_int
*/
int get_rand_int(int max_int){
    srand(time(0));
    return int(rand()) % max_int;
}


This program generates random addition problems
with integers between 0 and 100


int main(){
    int num1 = get_rand_int(101);
    int num2 = get_rand_int(101);
    cout << num1 << endl;
    cout << num2 << endl;

Please note this this is a homework problem my teacher assigned. He provided us with the skeleton and we're trying to fill in the blanks. I have a feeling the problem lies within the srand section of my function but that was part of my teacher's work so I'm not sure whether I need to touch that. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire