Jump to content

Random Number


The Little Guy

Recommended Posts

Duno if either are 'better' but they both do the job.

 

$arr = range(-50, 50);
$arr = array_filter($arr, create_function('$num', 'if ($num > -20 && $num < 20) return false; return true;'));
shuffle($arr);

echo $arr[0];

 

$options['options']['min_range'] = -20;
$options['options']['max_range'] = 20;

$done = false;

do
{
   $rand = mt_rand(-50, 50);
   
   if (filter_var($rand, FILTER_VALIDATE_INT, $options) !== false)
      $done = true;
      
}while (!$done);

Link to comment
https://forums.phpfreaks.com/topic/205818-random-number/#findComment-1077004
Share on other sites

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.