-
Newbie question
I'm new to this but i'm doing my best. Anyway i cant get the random function to work properly. I know it is in the FAQ but it doesnt help me.
Can you tell me what is wrong with this code?
Code:
#include <iostream.h>
#include <stdlib.h>
int main()
{
int numbers[6], loop;
for (loop=0; loop<6; loop++)
{
numbers[loop]=rand();
}
return 0;
}
Anyone? Thanks.
-
Re: Newbie question
You have to seed the random generator before using it. You may have to include time.h.
Also, rand() generates a random number in the range 0 - VERYLARGE, so if you just want a number say 0 - 9, use modulus, ie: rand() % 10.
Quote:
Originally posted by uNreal
Code:
#include <iostream.h>
#include <stdlib.h>
int main()
{
srand(time(NULL));
int numbers[6], loop;
for (loop=0; loop<6; loop++)
{
numbers[loop]=rand();
}
return 0;
}
Anyone? Thanks.
-
well, i look at it and see nothing wrong with it. it creates an array of 6 ints and stores random numbers in each. it has a few minor flaws, but it will compile and run and work. what do you think is wrong with it?
-
Ok thanks Magos i think that solved it.
moi the compiler wouldn't compile.........it didnt like it at all.
Thanks again!