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

Thread: Rand Number

  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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..

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $num mt_rand( -5) / 10
    Last edited by kbluhm; 07-16-2008 at 03:52 AM. Reason: Changed var name to $num to match your code. We all know people like to copy/paste examples.

  • Users who have thanked kbluhm for this post:

    Creekynoise (07-16-2008)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

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

    Creekynoise (07-16-2008)

  • #4
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    PHP Code:
    $num mt_rand( -5) / 10;
    print (
    "$num"); 
    That didn't work either =/

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

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No error here, copied/pasted your latest code directly and it works every time.
    Last edited by kbluhm; 07-16-2008 at 03:54 AM.

  • #6
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #7
    Regular Coder
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Creekynoise View Post
    PHP Code:
    $num mt_rand( -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.

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by Fou-Lu View Post
    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

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by rafiki View Post
    Did you miss this post? divide by 10.0
    Did you miss this post? Here it is again:
    Quote Originally Posted by kbluhm View Post
    No error here, copied/pasted your latest code directly and it works every time.
    This one too:
    Quote Originally Posted by Creekynoise View Post
    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.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 % 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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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