PHP Randoms – Random Functions To Do Random Numbers, Random Strings, Random Files, & More

By  on  

Random values are important to a lot of larger websites. Randoms can be used to generate strings for passwords, quotes, and CAPTCHAs. They can also be used for grabbing random numbers for games and other purposes. Here are a few random functions to do random things.

Random Integer

function get_random_number($min = 0, $max = 100) {
	return rand($min,$max);
}

Random String - From Preset Word List (Array)

function get_random_string_from_list($list_of_words) {
	return $list_of_words[rand(0, sizeof($list_of_words)-1)];
}

Random String

function get_random_string($length=6,$characters = "ABCDEFGHIJKLMNOPRQSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") {
        $num_characters = strlen($characters) - 1;
        while (strlen($return) < $length) {
            $return.= $characters[mt_rand(0, $num_characters)];
        }
        return $return;
}

Random Float

function random_float ($min, $max) {
   return ($min + lcg_value() * (abs($max-$min)));
}

Source: PHP.net

Random Hex Color

function get_random_hex_color($values = 'abcdef0123456789',$length=6) {
        $num_characters = strlen($characters) - 1;
        while (strlen($code) < $length) {
            $return.= $characters[mt_rand(0, $num_characters)];
        }
        return '#'.$return;
}

Random File From Directory

function get_random_file($dir) {
        while (false !== ($file = readdir($dir))) {
                $files[] = $file;
        }
        return $files[rand(0, sizeof($files)-1)];
}

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Xbox Live Gamer API

    My sharpshooter status aside, I've always been surprised upset that Microsoft has never provided an API for the vast amount of information about users, the games they play, and statistics within the games. Namely, I'd like to publicly shame every n00b I've baptized with my...

  • By
    Fancy FAQs with MooTools Sliders: Version 2

    A little over a year ago I authored a post titled Fancy FAQs with MooTools Sliders. My post detailed a method of taking boring FAQs and making them more robust using the world's best JavaScript framework: MooTools. I've taken some time to...

Discussion

  1. Victor Dotnet

    Wow! Nice tut Bro!

  2. vatsa

    Hi bro, I need help… I am trying to crate a project that would randomly sellect data e.g phone numbers from a database and then send sms to those same numbers with php. Thanks

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!