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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    HTML5 Context Menus

    One of the hidden gems within the HTML5 spec is context menus. The HTML5 context menu spec allows developers to create custom context menus for given blocks within simple menu and menuitem elements. The menu information lives right within the page so...

  • By
    Editable Content Using MooTools 1.2, PHP, and MySQL

    Everybody and their aerobics instructor wants to be able to edit their own website these days. And why wouldn't they? I mean, they have a $500 budget, no HTML/CSS experience, and extraordinary expectations. Enough ranting though. Having a website that allows for...

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!