php question, best way for random numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fris
    Too lazy to set a custom title
    • Aug 2002
    • 55679

    #1

    php question, best way for random numbers?

    Whats the best way for random number generation?

    this is what im using now

    Code:
    <?
    mt_srand ((double) microtime() * 1000000);
    $random = mt_rand(1,100);
    echo $random;
    ?>
    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.
  • HorseShit
    Too lazy to set a custom title
    • Dec 2004
    • 17513

    #2
    not sure but im sure our channel coders will know

    Comment

    • grumpy
      Too lazy to set a custom title
      • Jan 2002
      • 9870

      #3
      you dont need the mt_srand ((double) microtime() * 1000000);
      Its build in in the mt_rand
      Don't let greediness blur your vision | You gotta let some shit slide
      icq - 441-456-888

      Comment

      • Libertine
        sex dwarf
        • May 2002
        • 17860

        #4
        Originally posted by grumpy
        you dont need the mt_srand ((double) microtime() * 1000000);
        Its build in in the mt_rand
        Yep. As of 4.2, seeding is no longer needed, if I recall correctly.

        mt_rand() will do for most purposes, but if you need pseudo-random data for purposes related to cryptography, keep in mind that mt_rand uses a mersenne twister, which is rather weak. So, in those cases, getting your pseudo-random data from /dev/urandom is a better option.
        /(bb|[^b]{2})/

        Comment

        • leek
          Confirmed User
          • May 2008
          • 342

          #5
          Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
          RTFM

          Comment

          • Brujah
            Beer Money Baron
            • Jan 2001
            • 22157

            #6
            I believe that you don't really need to use mt_srand with php 5.2.1+
            You could use uniqid as a seed, or data from /dev/random

            Comment

            • Brujah
              Beer Money Baron
              • Jan 2001
              • 22157

              #7
              Originally posted by grumpy
              you dont need the mt_srand ((double) microtime() * 1000000);
              Its build in in the mt_rand
              True, but there was a fuss about it being insecure until 5.2.1+ I think

              Comment

              • fris
                Too lazy to set a custom title
                • Aug 2002
                • 55679

                #8
                im just getting a random number for random image headers, have 10 headers, want to rotate through them
                Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                Comment

                • Libertine
                  sex dwarf
                  • May 2002
                  • 17860

                  #9
                  Originally posted by fris
                  im just getting a random number for random image headers, have 10 headers, want to rotate through them
                  mt_rand will do fine, then.

                  The other stuff only comes into play when dealing with things where security is an issue, like when a forgotten admin password of a site gets reset on request (which actually was a security hole in Joomla and WP).
                  /(bb|[^b]{2})/

                  Comment

                  • yuu.design
                    Too lazy to set a custom title
                    • Mar 2006
                    • 25924

                    #10
                    Originally posted by fris
                    im just getting a random number for random image headers, have 10 headers, want to rotate through them
                    <?php
                    $image = rand(1,10);

                    echo "<img src=\"$image.jpg\" width=\"\" height=\"\" alt=\"\" />";

                    ?>

                    Beautiful And Usable Web Design Creations For The Adult Industry Since 2003
                    I'm Yuu, Designer and Content Producer

                    Paysites - Affiliate Programs - Dating & Cam Sites - Mainstream Projects - Tube Sites - Banners - Wordpress Themes - NATs integration - Landing Pages

                    Check my Portfolio and Content Production Offers

                    Comment

                    • Killswitch - BANNED FOR LIFE

                      #11
                      Like above, rand() will work fine for what you need.

                      Comment

                      Working...