rand() php code example

Example 1: random number generator in php

you can use rand() function for that in php.
Example:
Generate random numbers between 1 to 50
<?php
  echo rand(1,50);
?>

Example 2: php random number

// $min and $max are optional
rand($min,$max);

Example 3: php random integer

echo random_int(0,50);

Example 4: php random number

rand(0,10);
or
random_int(0,10)

Example 5: php rand int

random_int ( int $min , int $max );

Example 6: php rand vs mt_rand

// Since PHP7.1 rand() has become an alias of mt_rand()
// mt_rand() was supposed to be faster and more random than rand() in older PHP Versions
// That means you can use rand() instead of mt_rand() on newer Versions
<?php
  rand(1, 100);		// will generate a random number between 1 and 100
?>

Tags:

Php Example