Generating Random Values in PHP
Posted in: PHP

Generating Random Alphanumeric / Numeric Values in PHP

Hello friends hope you all are doing good and please be safe in this PANDEMIC situation. Wear Mask always. Generating random alphanumeric value in PHP can be done using some methods. All the methods have their own working style and output.  Do you know generating random values can help you to make OTP and Password’s

  1. rand($min, $max)
  2. mt_rand($min, $max)
  3. random_int($min, $max)
  4. rand_chars($length)
  5. generate_random_string($length)
  6. random_crypt_value($length)





rand($min,$max) – This function will allow us to get the value lie between minimum and maximum numbers. Previously rand() was bit slow but after the PHP 7 speed has been increased and now it works fast. rand() will help you to generate the numeric value between the minimum and maximum like rand(999,9999) will give you minimum value of 999 and max as 9999 or anything which lies in both the values. rand() is an int based function so you have to insert only integer value. You can also use it without giving any min or max number to the function.

Uses : – echo rand(5, 15); It will return anything like 9 or 10 etc.

mt_rand($min, $max) – This function will allow us to get the value lie between minimum and maximum numbers. Generate a random value via the Mersenne Twister Random Number Generator. mt_rand() will produce random numbers four times faster than what the average mt_rand() provides. mt_rand() will help you to generate the numeric value between the minimum and maximum like rand(0,999999) will give you minimum value of 0 and max as 999999 or anything which lies in both the values. But it will return the values 4 times faster then the rand().

Uses : – echo mt_rand(5, 15); It will return anything like 9 or 10 etc.

random_int($min, $max) – This function will allow us to generates cryptographically secure pseudo-random integers. It will generate random numbers between the provided $min and $max values, which default to PHP_INT_MIN and PHP_INT_MAX. Unfortunately, this function is only available starting from PHP 7.0.




rand_chars($length) –  This function is not the core PHP function. This is created by me so you have to use the whole function for using this.  This function will take the length as parameter so that it will create the random alpha of same length. It will choose the random value from 65 to 90 then change them to character using chr(). Function generate random alphabetic string.

Uses :- echo rand_chars(8);  It will return MBHJUIAS etc.

generate_random_string($length) – This function is not the core PHP function. It is created by me so you have to use the whole function for using this.  This function will take the length as parameter so that it will create the random alpha numeric value of same length.  This function generate random alphanumeric string.

Uses :- echo generate_random_string(8);  It will return AE1T8OM6 etc.

random_crypt_value($length) – This function is not the core PHP function.This is created by me so you have to use the whole function for using this.  This function will return an array with normal number and same number in encrypted format. $length will be int it will decide the length of the value.

Uses :- print_r(randomCryptValue(8));  It will return Array ( [1] => 19310775 [2] => $1$MtJ2rxll$kmqgwnXA9yZ01HkP05TVZ1 ) etc.

 




Hope you like the tutorial and our functions to generating random values as per need. If you have any advice please let me know comment below.  Please share it with others.

~ Stay Home Be Safe Use Mask ~

Leave a Reply

Your email address will not be published. Required fields are marked *