PHP | random_bytes () Function Last Updated : 14 Apr, 2022 Comments Improve Suggest changes Like Article Like Report The random_bytes()is an inbuilt function in PHP. The main function is to generate cryptographically secure pseudo-random bytes. It generates cryptographic random bytes of arbitrary string length. The different sources of randomness used in this function, they are as follows:- Window : CryptGenRandom() function.Linux : getrandom(2) system call function. Syntax : String random_bytes ( int $length ) Parameter : It is the length of random string, returned in bytes. Return Value: The function returns the cryptographically secure random bytes in string. Examples: Input : length = 7 Output :string(14) "cbd392c01352b0" Below programs illustrate the random_bytes () function in PHP. Program 1: php <?php //random_bytes () function in PHP $length = random_bytes('4'); //Print the result and convert by binaryhexa var_dump(bin2hex($length)); ?> Output:string(8) "e62a94a2" php <?php //random_bytes () function in PHP $length = random_bytes('6'); //Print the result and convert by binaryhexa var_dump(bin2hex($length)); ?> Output:string(12) "808fc44d325b" Exception Error : Invalid parameter will give TypeError .Invalid length of bytes gives Error.If source of randomness is not found then Exception will be thrown. References: http://php.net/manual/en/function.random-bytes.php Comment More infoAdvertise with us Next Article PHP | random_bytes () Function J jit_t Follow Improve Article Tags : Web Technologies PHP Similar Reads PHP | gmp_random_bits() Function The gmp_random_bits() function is an inbuilt function in PHP which generates a random number. The random number will thus be between the range 0 and (2 * bits) - 1. Here bits must be greater than 0, and the maximum value of bits is restricted by available memory. Here GMP refers (GNU Multiple Precis 2 min read PHP rand() Function In this article, we will see how to get the random number using the rand() function in PHP, along with knowing its implementation through the example. The rand() is an inbuilt function in PHP used to generate a random number ie., it can generate a random integer value in the range [min, max]. Syntax 2 min read PHP | random_int() Function The random_int () is an inbuilt function in PHP. The main function is to generate cryptographically secure pseudo-random integers value. When unbiased results occur in critical condition, then generated cryptographic random integers are used.The different sources of randomness used in this function 2 min read PHP array_rand() Function This inbuilt function of PHP is used to fetch a random number of elements from an array. The element is a key and can return one or more than one key. On a practical basis, this is not that useful because the function uses pseudo-random number generator that is not suitable for cryptographic purpose 2 min read PHP | gmp_random() Function The gmp_random() function is an inbuilt function in PHP which generates a random number. The range of random number will be in between zero and the number of bits per limb ( A limb is an internal GMP mechanism. The number of bits in a limb is not static and it can vary from system to system. Usually 2 min read Like