Using the math functions

Functions that generate random numbers

rand($min, $max)
mt_rand($min, $max)
random_int($min, $max)

How to simulate the roll of a six-sided die

<?php
  $die = random_int(1, 6);
?>

How to generate a random value between 0 and 1 with 5 decimal places

<?php
  $number = 0;
  $places = 5;
  for($i = 0; $i < $places; $i++) {
    $number += mt_rand(0,9);
    $number /= 10;
  }
  echo $number;
?>

Back