rand() function

This is a discussion on rand() function within the C Programming forums, part of the General Programming Boards category; Hi, this is my first post...great site...i want to ask sth...i made a small program but i have a problem. ...

rand() function

Hi, this is my first post...great site...i want to ask sth...i made a small program but i have a problem. when we call rand() we get a number betwen 0 and RAND_MAX. I want to have numbers betwwen 0 and 1. Does anyone know how can i do this? The problem is:
#...
#...
main(){
int x;
srand(5);
x = rand();
x = rand()/RAND_MAX; //output is always 0...

rand() function

thx v33k....it is working...i can now have numbers bettween 0 and 1...but could you make a program with rand + srand where i can have results like these: (after the . i want only one digit)
e.g.:
output:
0.5
0.6
0.1
0
0
0.8
0.9
0.5
0.6
0.7
0.8
0.4
0.6
i wish you understood what i want...i am working C about 9 months but i miss some simple things..so if sb could help me i would appreciate this...

Floats/doubles are actually bad for 0.1, 0.2, 0.3, because a tenth of a unit is not a "power of two number". If you want fixed precision here, you are better to use an int between 0 and 10, like DeadPlanet says, then when you want to output it as a decimal:

Code:

int x;
printf("%.1f",(float)(x/10));

This should be fine, because printf by default rounds correctly (to nearest, not down). The reason it may have to round is that one tenth is not exactly represented in memory, as just mentioned. For an illustration of that, run this:

@sandeep080:
First of all, read the rest of the topic. Your post is a bit... late. But it's even wrong, as rand() returns an integer and RAND_MAX is an integer, meaning that you will divide two integers, and the result will be an integer, not a float.