| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random Number with Range
Hi,
How would I create a random number within a certain range like 0-10, but also include the decimal values as well? Such as 1.1, 3.2. 5.7, etc. Thanks |
|
#2
|
|||
|
|||
|
Just get a random number between 1 and 100, then divide it by 10. That will give you a random number between 1 and 10 including decimals out to the tenths place.
|
|
#3
|
||||
|
||||
|
Actually, you'll need to mod the random number by the maximum number, it gives better range
Just generate a random number between 1 and 100, then multiply it by .1, thus if you generated 11, it would become 1.1, if it generated 32 it would become 3.2, etc....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#4
|
|||
|
|||
|
Remember that if you need a true random number, srand is NOT going to give it to you,
|
|
#5
|
|||
|
|||
|
there is a random number generator function in c library.
if you want to generate up to 10 random numbers then simple write rand() % 11 and if you want to generate from 0 to 10 then the syntax for this is rand() % ((max +1) - min)) + min |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Random Number with Range |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|