# Thread: problems with generating random numbers

1. ## problems with generating random numbers

Hi everybody,
I have a problem with a fuction that should generate a random number between 0 and 1 but it only generates 0 everytime. i dont know what's wrong anymore, if someone could help me i would be greatful. Thanks. Here is the code:
float generare(void)
{
int r;
float U;
randomize();
r=random(100);
U=r/99;
return U;

2. Where do the functoins random() and randomize() come from?

In C you can include the time.h and stdlib.h header files and seed like this
Code:
`srand( time( NULL ) );`
and generate random numbers like this
Code:
`randNum = rand() % 100 + 1`

3. ## Re: problems with generating random numbers

>>problem with a fuction that should generate a random number between 0 and 1 but it only generates 0 everytime.

http://www.eskimo.com/~scs/C-faq/q13.18.html
http://www.eskimo.com/~scs/C-faq/q13.16.html

4. >>I have a problem with a fuction that should generate a random number between 0 and 1
The solution is really very simple :-)
Code:
```#include <stdio.h>
#include <stdlib.h>

static double generate_rand(void)
{
return (double)rand() / RAND_MAX;
}

int main(void)
{
int i;

for (i = 0; i < 20; i++)
{
printf("%f\n", generate_rand());
}

return 0;
}```
You can seed it if you'd like also.

5. ## This may work

I think the problem with your code is that the result of the randomize() should be stored in a float. Try the following code -
Code:
```float r,u;
r = randomize(100);
u = r/99.0;
printf("%f",u);```

6. ## Sorry

Code:
`r = random (100);`

7. Thanks everybody,
I still see my version as being the easiest, i just haven't found yet the mistake. Probably isn't a big mistake, that's why i cant see it.
Oh, and thanks Code Jerk but it cant work with float, randomize and random are function that return an integer. Look up into the C library, stdlib. Anyway, any idea is welcomed, i'm still trying to find the mistake.
Thank you all.

8. You have r as an int, which you then divide by 99 and get, shock horror! 0
Make r a float, then see what happens.

9. here is the code again:
float generare(void)
{
int r;
float U;
randomize();
r=random(100);
U=r/99;
return U;
So, randomize initialize the random generator. Then, r gets an integer number between 0 and 99. But i need a random number between 0 and 1 so, i make U=r/99, U is float as the result. Now, if i erase the U=r/99 line, things are ok, meaning the function works and i get a random number between 0 and 99. But i need more than that and sounds like the problem is with that U float. I've had problems with floats in C and Pascal too some other times but in the end i made things work. this time i dont get it and also i'm pretty tired.
Again, thanks alot all of you there.

10. Change this:
>U=r/99;

... to this ....

>U=r/99.0;

11. ## Thank you all!!!

Hey there,
Sorry it toke me so long to reply but i was caught into some exams. First of all i want to thank you all here for your help. Second, Hammer, thank you, that was the problem, a dot and a zero. My project was cool and you helped me when my brain got stucked. Thank you all and hope i'll help you too one day.
Ioana.