使用PHP在范围内的随机数[最小-最大值]

时间:2022-09-28 16:11:06

Is there a way to generate a random number based on a min and max?

有没有一种方法可以根据最小值和最大值生成随机数?

For example, if min was 1 and max 20 it should generate any number between 1 and 20, including 1 and 20?

例如,如果最小值是1,最大值是20,那么它应该生成1到20之间的任何数,包括1和20?

7 个解决方案

#1


111  

<?php
  $min=1;
  $max=20;
  echo rand($min,$max);
?>

#2


16  

A quicker faster version would use mt_rand:

更快的版本将使用mt_rand:

$min=1;
$max=20;
echo mt_rand($min,$max);

Source: http://www.php.net/manual/en/function.mt-rand.php.

来源:http://www.php.net/manual/en/function.mt-rand.php。

NOTE: Your server needs to have the Math PHP module enabled for this to work. If it doesn't, bug your host to enable it, or you have to use the normal (and slower) rand().

注意:您的服务器需要启用Math PHP模块才能正常工作。如果没有,请bug您的主机以启用它,或者您必须使用正常的(和更慢的)rand()。

#3


16  

In a new PHP7 there is a finally a support for a cryptographically secure pseudo-random integers.

在新的PHP7中,最终支持加密安全的伪随机整数。

int random_int ( int $min , int $max )

random_int — Generates cryptographically secure pseudo-random integers

random_int -生成加密安全的伪随机整数

which basically makes previous answers obsolete.

这基本上使以前的答案过时了。

#4


5  

(rand() % ($max-$min)) + $min

or

rand ( $min , $max )

http://php.net/manual/en/function.rand.php

http://php.net/manual/en/function.rand.php

#5


4  

rand(1,20)

Docs for PHP's rand function are here:

PHP rand函数的文档如下:

http://php.net/manual/en/function.rand.php

http://php.net/manual/en/function.rand.php

Use the srand() function to set the random number generator's seed value.

使用srand()函数设置随机数生成器的种子值。

#6


4  

I have bundled the answers here and made it version independent;

我把答案捆在这里,使它独立于版本;

function generateRandom($min = 1, $max = 20) {
    if (function_exists('random_int')):
        return random_int($min, $max); // more secure
    elseif (function_exists('mt_rand')):
        return mt_rand($min, $max); // faster
    endif;
    return rand($min, $max); // old
}

#7


0  

Try This one. It will generate id according to your wish.

试试这个。它将根据您的意愿生成id。

function id()
{
 // add limit
$id_length = 20;

// add any character / digit
$alfa = "abcdefghijklmnopqrstuvwxyz1234567890";
$token = "";
for($i = 1; $i < $id_length; $i ++) {

  // generate randomly within given character/digits
  @$token .= $alfa[rand(1, strlen($alfa))];

}    
return $token;
}

#1


111  

<?php
  $min=1;
  $max=20;
  echo rand($min,$max);
?>

#2


16  

A quicker faster version would use mt_rand:

更快的版本将使用mt_rand:

$min=1;
$max=20;
echo mt_rand($min,$max);

Source: http://www.php.net/manual/en/function.mt-rand.php.

来源:http://www.php.net/manual/en/function.mt-rand.php。

NOTE: Your server needs to have the Math PHP module enabled for this to work. If it doesn't, bug your host to enable it, or you have to use the normal (and slower) rand().

注意:您的服务器需要启用Math PHP模块才能正常工作。如果没有,请bug您的主机以启用它,或者您必须使用正常的(和更慢的)rand()。

#3


16  

In a new PHP7 there is a finally a support for a cryptographically secure pseudo-random integers.

在新的PHP7中,最终支持加密安全的伪随机整数。

int random_int ( int $min , int $max )

random_int — Generates cryptographically secure pseudo-random integers

random_int -生成加密安全的伪随机整数

which basically makes previous answers obsolete.

这基本上使以前的答案过时了。

#4


5  

(rand() % ($max-$min)) + $min

or

rand ( $min , $max )

http://php.net/manual/en/function.rand.php

http://php.net/manual/en/function.rand.php

#5


4  

rand(1,20)

Docs for PHP's rand function are here:

PHP rand函数的文档如下:

http://php.net/manual/en/function.rand.php

http://php.net/manual/en/function.rand.php

Use the srand() function to set the random number generator's seed value.

使用srand()函数设置随机数生成器的种子值。

#6


4  

I have bundled the answers here and made it version independent;

我把答案捆在这里,使它独立于版本;

function generateRandom($min = 1, $max = 20) {
    if (function_exists('random_int')):
        return random_int($min, $max); // more secure
    elseif (function_exists('mt_rand')):
        return mt_rand($min, $max); // faster
    endif;
    return rand($min, $max); // old
}

#7


0  

Try This one. It will generate id according to your wish.

试试这个。它将根据您的意愿生成id。

function id()
{
 // add limit
$id_length = 20;

// add any character / digit
$alfa = "abcdefghijklmnopqrstuvwxyz1234567890";
$token = "";
for($i = 1; $i < $id_length; $i ++) {

  // generate randomly within given character/digits
  @$token .= $alfa[rand(1, strlen($alfa))];

}    
return $token;
}