Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Rand Number

I need a script that will generate a random number between -.5 and +.05

I've tried this:

PHP Code:
``` \$num = rand(-.05, .05); print ("\$num");  ```
but it hasn't worked..

• PHP Code:
``` \$num = mt_rand( -5, 5 ) / 10;  ```

• ## Users who have thanked kbluhm for this post:

Creekynoise (07-16-2008)

• Gotta tell them why mate, otherwise nobody learns anything :P

rand function require integer results. 0.5 and -0.5 are equal to 0, so you'll never receive a correct number. Instead, as kblumm used seek between 5 and -5 and divide you're result by 10. Don't forget to divide by 10.0 - otherwise you're result will be a 0.

• ## Users who have thanked Fou-Lu for this post:

Creekynoise (07-16-2008)

• PHP Code:
``` \$num = mt_rand( -5, 5 ) / 10;print ("\$num");  ```
That didn't work either =/

Edit: Tried flipping the -5 and 5 around too..still get an error..

• No error here, copied/pasted your latest code directly and it works every time.

• Ok it works for me now, I tried it in a new file..I guess some other code was conflicting with it, but it works, thanks!

• Originally Posted by Creekynoise
PHP Code:
``` \$num = mt_rand( -5, 5 ) / 10;print ("\$num");  ```
That didn't work either =/

Edit: Tried flipping the -5 and 5 around too..still get an error..
Well what's the error?

EDIT: Nevermind, guess it's working.

• Originally Posted by Fou-Lu
Don't forget to divide by 10.0 - otherwise you're result will be a 0.
Did you miss this post? divide by 10.0

• Originally Posted by rafiki
Did you miss this post? divide by 10.0
Did you miss this post? Here it is again:
Originally Posted by kbluhm
No error here, copied/pasted your latest code directly and it works every time.
This one too:
Originally Posted by Creekynoise
Ok it works for me now, I tried it in a new file..I guess some other code was conflicting with it, but it works, thanks!
Not sure why the suggestion was made to divide by a float, it is unnecessary.

• PHP is a datatype weak language. Theoretically 5 / 10 should result in 0, not in 0.5 since both of the datatypes provided are integers. PHP however attempts to find the closest matching datatype. This is actually a problem in PHP since it requires the developer to make consistency checks on all of their results. Should you ever land on a \$x &#37; 10 = 0 number, you will have an end result that is a integer, not a double. float 0 !== int 0, which forces checks on 0, not 0.0, or explicit casting to float/double. This destroys the integrity of you're variable data as you no longer know what datatype it contains.

Choosing to divide by 10.0 implicitly casts to a float, so even an \$y = %x % 10 = 0 number will result in a float(\$y). You can always fit an integer into a float, but you can never fit a float into an integer. Try doing this in a datatype strong language like C/C++, java, C# etc. and you'll see what I'm talking about.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•