# Thread: Is it possible to generate a certain number of random numbers in C?

1. ## Is it possible to generate a certain number of random numbers in C?

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?

Code:
```for(x=0; x<VALUE; x++)
{
srand ( time(NULL) );
arr[x] = rand() % 10 + 1;
printf("--> %d\n", arr[x]);
}```
Let's say VALUE is 10. So, the program should generate 10 different numbers. But, that wasn't what i got.
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  2. you only need to call srand() once

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 3. Originally Posted by soawperL25 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.
Call srand() once, and only once, before the loop. Originally Posted by soawperL25 One last thing, how do I generate negative numbers?
If you multiply positive values by -1, the result will be negative.

Subtracting 10 from a set of values between 0 and 25 will produce results between -10 and 15.

In general, rand() is specified to produce a value between 0 and RAND_MAX. If you have some other requirement, work out some transformation that, for every value between 0 and RAND_MAX, produces a value that meets your needs.

Given your difficulties doing this in C (it is actually quite simple) the mind boggles at what you consider is difficult about doing it in Java. 4. Wow, thank you very much! That really helps. So, I finally changed it into this
Code:
```srand ( time(NULL) );
for(x=0; x<VALUE; x++)
{
arr[x] = rand() % 20 * -1 + 10;
printf("%d\t", arr[x]);
}```
Thanks again, everyone!  5. Originally Posted by soawperL25 Wow, thank you very much! That really helps. So, I finally changed it into this
Code:
```srand ( time(NULL) );
for(x=0; x<VALUE; x++)
{
arr[x] = rand() % 20 * -1 + 10;
printf("%d\t", arr[x]);
}```
Thanks again, everyone! I am giving you an additional exercise:

* Explain why moving the srand() outside of the loop changed what is printed; i.e. why did doing this fix the code? 6. Originally Posted by Hodor I am giving you an additional exercise:

* Explain why moving the srand() outside of the loop changed what is printed; i.e. why did doing this fix the code?
I guess that is because srand() is the function which generates the time for random the number. So, when I moved it out it generates different number for each loop? Lol, no. Can't really figure it out. So, what is it? 7. Read srand - C++ Reference

Hint: Your program was running real fast compare to the units used in the time function. Popular pages Recent additions random number 