random number
I was trying to produce random numbers to input as coordinates, but the random number keeps returning the same value for both x and y.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream.h>
#include <time.h>
int random(int);
void main()
{
int x,y,max;
cin >> max;
x=random(max);
y=random(max);
cout << x << ' ' << y; // Keeps displaying the same value
}
int random(int max)
{
srand(time(0));
return rand()%max;
}
|
Last edited on
Am I misunderstanding some concept in random numbers?
Note: This wouldn't compile for me with your code. So I have modified it to be more platform friendly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <time.h>
using namespace std;
int random(int);
int main() {
srand(time(0));
int x,y,max;
cout << "Input Max:";
cin >> max;
x=random(max);
y=random(max);
cout << x << ' ' << y; // Keeps displaying the same value
return 0;
}
int random(int max) {
return rand()%max;
}
|
yay it works. thanks for your help =D
the problem was the position of srand, now it only gets called once.
Topic archived. No new replies allowed.