PHP – función rand () : En este artículo, vamos a aprender sobre función rand () , cómo generar números aleatorios entre min y max?
Dada min y el número máximo y tenemos que imprimir número aleatorio entre ellos utilizando la función de PHP rand ().
rand () función se utiliza para generar números aleatorios en PHP, es una función incorporada y se puede utilizar para conseguir cualquier número aleatorio entre rango dado.
Sintaxis:
rand(min, max);
Aquí,
- min – define el alcance mínimo del número aleatorio.
- max – define el alcance máximo del número aleatorio.
Nota: Si no se especifica rangos, rand () va a generar números aleatorios entre 0 y getrandmax () .
código PHP
<?php
//Generating random number from 0 to genrandmax()
$num = rand();
print_r('Random number is: ' . $num . "n");
//second time
$num = rand();
print_r('Random number is: ' . $num . "n");
//Generating random number from 100 to 200
$num = rand(100,200);
print_r('Random number is: ' . $num . "n");
//second time
$num = rand(100,200);
print_r('Random number is: ' . $num . "n");
?>
salida
Random number is: 1399397815
Random number is: 453651028
Random number is: 175
Random number is: 185