I got a homework for a class where we're supossed to use Java. But since we just started the teacher let us to use C too if using Java is too difficult. I gave up with Java. So, I wrote in C instead. I know in Java it's possible to generate a number of random numbers, but how about in C? My program is almost complete. The only problem is I don't know how to generate random numbers.
the program is supposed to generate a number of random numbers. So, do I need to use loop like this?
Let's say VALUE is 10. So, the program should generate 10 different numbers. But, that wasn't what i got.Code:for(x=0; x<VALUE; x++) { srand ( time(NULL) ); arr[x] = rand() % 10 + 1; printf("--> %d\n", arr[x]); }
Instead, I got something like this.
--> 10
--> 10
--> 10
so on...
or
--> 5
--> 5
--> 5
so on..
It generates the same numbers! I really do not know way. it is the first time I use srand() and rand(). So, please someone tell me the mistake.
One last thing, how do I generate negative numbers?
Thanks in advance