简体   繁体   中英

How do I generate a random range of numbers within another range?

Apologies for the confusing title, I couldn't phrase it much differently.

I want to be able to generate a random range of x number(s) within the number 10000.

Such as 200-500 (201, 202, 203... ...499) or 9700 to 10000.

I also want the function to be easily changed so it will be a range of x-amount numbers. The above was a range of 200, x = 200... I would like to be able to easily change that.

Thank you.

Makes use of range() :

$min = 1;
$max = 10000;

$nb = 300;

// in this example, $start can be from 1 to 9701
$start = mt_rand($min, $max - $nb + 1);

// in this example, $result can be from [1;300] to [9701;10000]
$result = range($start, $start + $nb - 1);

尝试mt_rand()

echo mt_rand(0, 10000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM