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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mt_rand returning negative number

    Hi
    How come my mt_rand returns negative numbers while it shouldn't?
    PHP Code:
    <?
    //SMALL MT_RAND COMPATIBILITY TEST
    $echoout=mt_rand(1000000000000,22000000000000);
    echo
    "$echoout";
    ?>
    Sometimes it gives me a positive number (i once had : 75587147 ) but mostly i get things like -216672647
    I think it's because the numbers are so big. Is there a function that creates a random number between two bcmathic numbers. I already use bcmathic functions like bcmul and bcmod etc on my page because of the high numbers. Is there a function that can create a random number between to big numbers?

    THank you very much
    P.S. It shouldn't be a very 'difficult' function, as simple as possible as i will have to execute it a lot of times, so it shouldn't take too long to calculate!

    THANK YOU VERY MUCH FOR YOUR HELP!

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any help would be appreciated

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    your numbers are too big,run...
    PHP Code:
    <? 
    echo mt_getrandmax();
    ?>
    to find out the larget possible random value you can get (will probably be 2147483647)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    also check out gmp_random()
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes my numbers are too big, but what can i do about it?
    Is there something else than gmp_random because it works with bits and it's not what i'm looking for!

  • #6
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone?

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    A simple example:
    PHP Code:
    function arbitrary_rand($min$max)
    {
        return 
    gmp_add($mingmp_mod(gmp_random(), $max));

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you for your help but i'm having a problem : i think the GMP libary is not activated/included on the hosting server i'm on as the php page that uses it tells me : 'undefined function gmp_add'

    Is there another solution? Thanks anyway!

  • #9
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is there a way I don't need the GMP library installed to get it to work?
    Is there another function that can help me out here, please?


  •  

    Posting Permissions

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