random number between 0&1 in C?

Status
Not open for further replies.

legolas

Padawan
hi,

the inbuilt function rand() generates a large integer that i hav to use rand()%x to get a number 0 & x including 0.

how do i generate a number a > 0 and < 1 using the rand function. i knew its not that difficult, either divide the rand() by a number larger.. or divide by another rand() by checking if its greater than the first number generated. but i wanted to know if there is any inbuilt command or any other better algorithm for generating. thk u.

/legolas
 

thinkdigital

Right off the assembly line
there is no inbuilt function...

in my knowledge... there is no as such in built function. i'll surely try that out ... thanx for giving me a good problem to think.
 

sakumar79

Technomancer
For 0 to 1 you can use x = (float) rand()/RAND_MAX;
cstdlib has a constant RAND_MAX that represents the largest possible value to be generated. So, just use it...

remember to seed your random number using time as parameter in order to get good variation.

For further details, refer *cplus.about.com/od/advancedtutorials/l/aa041303c.htm

Arun
 
Status
Not open for further replies.
Top Bottom