Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.