Now: Tutorial for Web and Software Design > PHP > QA > PHP Content
> PHPFAQ - Why doesnt rand give random numbers? [Bookmark it]
PHPFAQ - Why doesnt rand give random numbers?


Question :

Why doesn't rand give random numbers?

Answer :I use the following code

$iRandom = rand(0,100);

But I dotn get random numbers, whats wrong?

You have not RTFM'ed good enough, if you read the man page for rand carefully you will find a line that states:

"Remember to seed the random number generator before use with srand()."

So the proper code would be:

srand((double)microtime()*1000000); 
$iRandom = rand(0,100);


As an alternative you can use mt_rand which should generate more random, numbers. Read more about that here:
http://www.php.net/manual/en/function.mt-rand.php



[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    PHPFAQ - How google is you
    PHPFAQ - Are there any web
    PHPFAQ - How can I accept 
    PHPFAQ - How can I finance
    PHPFAQ - Where can I get P
    PHPFAQ - Where can I host 
    PHPFAQ - Where can I find 
    PHPFAQ - I am trying to cr
    PHPFAQ - Is there a librar
    PHPFAQ - How can I manage 
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial