How to Use the PHP rand() Function ?

December 15, 2018

The rand() function generates a random integer. the rand() function used  to generate a random number within a specify range, like a number between 10 to 50. How to use the PHP rand() function ? and How to generate a random number in php. 

This rand() function does not generate cryptographically secure values, and should not be used for cryptographic purposes. if you need secure values, use Other Function such as a random_int(), openssl_random_pseudo_bytes(), or random_bytes(). 

in Windows the maximum value of rand() function is only 32767. if you want to generate number larger then 32767. You have to use the min and maximum value to Generate the Number Larger then 32767.

Rand() Syntax & Example 

The Correct Syntax for rand() function is below

and with minimum and maximum syntax is below

if you want print a integer you have to add below line

If You want Generate a number between two numbers. Lets assume we have to generate random number between 10 to 50. then Syntax is

In PHP 7 the rand() PHP function is an alias of mt_rand(). mt_rand() is same as rand() function. mt_rand() is more 4 time faster than rand() function. 

Karan Suthar

Surat, India

I'm Karan, the creator Behind the KTechBlog. I write Blogs in my Free Time. I ❤ to learn new Technologies and Share with Others. I'm Trying best effort to make KTechBlog useful for Everyone.