+ Reply to Thread
Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By cursedpsp
  • 1 Post By hamsn

Thread: Random numbers in PHP

  1. #1
    cursedpsp is offline x10 Sophmore cursedpsp is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wiltshire, England
    Posts
    238

    Random numbers in PHP

    Hi, ill show you how to use 'mt_rand' 'srand' and a really complicated one that makes hexadecimal numbers.

    lets start off with the 'mt_rand' function.
    Code:
    //Minimum number
    $min = 1;
     
    //Maximum number
    $max = 100;
     
    //Put mt_rand into a variable
    $random = mt_rand($min, $max);
     
    echo "Our random number of the day is: " . $random;
    Now for the 'srand' function.
    Code:
    //Minimum number
    $min = 1;
     
    //Maximum number
    $max = 100;
     
    srand((double) microtime()*1000003);
    //1000003 because it is a prime number
     
    //convert it to a variable
    $random = rand($min, $max);
     
    echo "Another random number today is: " . $random
    I dont actually know what the difference it to the random number functions but they do their job .

    The next one will create a hexadecimal code which is very useful for sessions and cookies, so that people will never guess them!
    Code:
    $hex = sha1(uniqid(mt_rand()));
     
    echo "Todays impossible to guess hex code is: " . $hex;
    echo
    dinomirt96 likes this.

  2. #2
    cursedpsp is offline x10 Sophmore cursedpsp is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wiltshire, England
    Posts
    238

    Re: Random numbers in PHP

    There is a way to increase the hex digits but i dont know how to - anyone?

  3. #3
    hamsn's Avatar
    hamsn is offline x10 Lieutenant hamsn is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Earth™
    Posts
    290

    Re: Random numbers in PHP

    where will this be useful?
    i mean like can we use it in visitor counter
    no. of seconds logged in


    or else...ideas?
    karimirt47 likes this.
    I Rise, You fall.

  4. #4
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Random numbers in PHP

    I don't think this is really a good tutorial to be honest...
    mt_rand() is just a function like rand(), you need 30 seconds to look it up at php.net...
    And about the second srand() and rand() thing...
    http://be.php.net/manual/en/function.srand.php
    "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."
    So you don't need to call srand() at all...
    And sha1() is just a form of encoding stuff, using the secure hash algorithm 1, it has nothing to do with random numbers...

    Sorry, but if you make a tutorial, please inform yourself properly...
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  5. #5
    bigjoe4 is offline x10 Elder bigjoe4 is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    907

    Re: Random numbers in PHP

    lol that is cool I will definately put that on my site
    Please visit my site: Circuit designer

  6. #6
    cursedpsp is offline x10 Sophmore cursedpsp is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wiltshire, England
    Posts
    238

    Re: Random numbers in PHP

    Quote Originally Posted by hamsn View Post
    where will this be useful?
    i mean like can we use it in visitor counter
    no. of seconds logged in


    or else...ideas?
    Random advertisement,

    Those "random joke of the day" things use it.

    Anything that you want to be random

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Random numbers in PHP

    Like a confirmation code in a forum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    cursedpsp is offline x10 Sophmore cursedpsp is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wiltshire, England
    Posts
    238

    Re: Random numbers in PHP

    Blimey, i found this atlast - i was searching for days for this (even using the search bar)

  9. #9
    sunils's Avatar
    sunils is offline x10 Spammer sunils is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Chennai ,India
    Posts
    2,264

    Re: Random numbers in PHP

    A good post. will be helpfull to the newbie..
    [LEFT][B]Sunil Sankar
    -------------------------------------------------------------------------

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  2. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  3. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers