Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 13-10-2005, 09:10 AM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default random number between 0&1 in C?


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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 14-10-2005, 01:53 AM   #2 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2004
Posts: 9
Default 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.
__________________
Keep Rocking!!

Varun Nair
thinkdigital is offline  
Old 14-10-2005, 07:08 AM   #3 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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 http://cplus.about.com/od/advancedtu.../aa041303c.htm

Arun
sakumar79 is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by topgear
- by Charan

Advertisement




All times are GMT +5.5. The time now is 06:47 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2