how to change random number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int generate_random(int ran);

int main(int random)
{
    int your_number = generate_random(random);
    cout << generate_random(random) <<endl;
    cout << generate_random(random) <<endl;
    cout << "Your number is "<< your_number <<endl;
    cout << "Your number is "<< your_number + 1 <<endl;

}

int generate_random(int i)
{
        int max = 100;

    srand(time(0));
    i = (rand() % max) + 1;

    return i;
}


how can i change line 12 to indicate a different generated random number with the same function?
Last edited on
Um... what exactly is your problem?

-Albatross
Last edited on
Actually he remember it too much, xP.
Just make one call in the whole program. A good place will be the beginning of main.
Oh sorry I did not notice the srand in the generate function and as ne555 said you should use srand only once.
Again sorry for not noticing the srand.
Last edited on
i dont get it??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int generate_random(int ran);

int main(int random)
{
    int your_number = generate_random(random);
    cout << generate_random(random) <<endl;

    cout << "Your number is "<< your_number - 1<<endl;
    cout << "Your number is "<< your_number + 1 <<endl;

    srand(1);
    cout << generate_random(random) <<endl;

}

int generate_random(int i)
{
        int max = 100;

    srand(time(0));
    i = (rand() % max) + 1;

    return i;
}
Direct your attention to the comment below mine, Zhuge is right.

Instead of having srand(time(0)) in the function, put it in the beginning of main.
Last edited on
Or you could simply call srand(time(0)) only once at the beginning of your program.
Topic archived. No new replies allowed.