-
Randomisation problems
I am having trouble telling the computer to select 3 random numbers and write them out.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
int Chance1 , Chance2 , Chance3;
void main(void)
{
cout << "These 3 numbers should be different: \n";
{
srand( (unsigned) time (NULL) );
Chance1 = rand() % 10;
cout << Chance1 << "\n";
srand( (unsigned) time (NULL) );
Chance2 = rand() % 10;
cout << Chance2 << "\n";
srand( (unsigned) time (NULL) );
Chance3 = rand() % 10;
cout << Chance3 << "\n";
}
}
There should be three number and each should be different, but some how they all come out the same number!
Any help would be most appreciated,
Paul
-
You only need to call srand once in your program
this should be:
which means you need to put a return 0 before the closing brace of main as well
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
int Chance1 , Chance2 , Chance3;
int main(void)
{
cout << "These 3 numbers should be different: \n";
srand( (unsigned) time (NULL) );
Chance1 = rand() % 10;
cout << Chance1 << "\n";
Chance2 = rand() % 10;
cout << Chance2 << "\n";
Chance3 = rand() % 10;
cout << Chance3 << "\n";
return 0;
}
And as your only wanting numbers 0-9 your gonna get the same numbers repeated quite a lot.