Jump to content

[SOLVED] rand (1000, 1025)(2000,2025)(3000,3025)


markvaughn2006

Recommended Posts

On the rand() function is there a way to have it pick a random number between say... 1000-1025 AND 2000-2025 AND 3000-3025, etc... so possible outcomes could be 2014, 1023, 1005, 3002, etc... all from this one rand()

 

I know thats weird , just have a specific reason for wanting to do this... Thanks in advance!!

Link to comment
https://forums.phpfreaks.com/topic/178525-solved-rand-1000-10252000202530003025/
Share on other sites

As salathe said there isn't with a single call to rand(), but you could use something like:

 

$ranges = array(
    0 => array(1000, 1025),
    1 => array(2000, 2025),
    2 => array(3000, 3025)
);

$n = array_rand($ranges);

echo rand($ranges[$n][0], $ranges[$n][1]);

I'll throw in another solution:

 

$values = array_merge(range(1000, 1025), range(2000, 2025), range(3000, 3025));
$rand_value = $values[array_rand($values)];

 

EDIT: MrAdam went with a similar solution, but mine picks a single random value out of the entire list instead of first selecting one of three lists and then selecting a value out of that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.