1984
1984

Reputation: 297

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.

Upvotes: 1

Views: 633

Answers (2)

Gras Double
Gras Double

Reputation: 16373

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);

Upvotes: 1

Machavity
Machavity

Reputation: 31614

Try mt_rand()

echo mt_rand(0, 10000);

Upvotes: 0

Related Questions