I have a function to spit out random locations with XYZ space. I am getting a 500 internal server error whenever my radius exceeds 1e 06. My cuboid location populator doesn't seem to have the same issue, which it simply compares new locations to the dimension defined as maximum limits. I am not really great with maths and suspect I am doing something wrong with calculations, which may be killing PHP with too large of integers or something? It is a 64bit system, however.
/*
* $loc = Point of Origin
* $r = Maximum radius
* $ylimit = Limit locations on Y Axis
*/
function basic_spheroid_point( $loc, $r, $ylimit = 0 ) {
list ($cx, $cy, $cz) = $loc;
static $cnt = 0;
$dx = randint( -$r, $r );
$dy = randint( -$r, $r );
$dz = randint( -$r, $r );
$dd = sqrt( $dx * $dx $dy * $dy $dz * $dz );
if ( $dd > $r ) {
$cnt ;
if ( $cnt <= 10000 )
return basic_spheroid_point( $loc, $r, $ylimit );
}
$cnt = 0;
$dx = $r * $dx / $dd;
$dy = $r * $dy / $dd;
$dz = $r * $dz / $dd;
$loc = array( $cx $dx, $cy $dy, $cz $dz );
if ( $ylimit > 0 ) {
if ( $loc[1] < $ylimit && $loc[1] > -$ylimit ) {
return $loc;
} else {
return basic_spheroid_point( $loc, $r, $ylimit );
}
} else {
return $loc;
}
}
function randint($min = 0, $max = 0) {
if ( function_exists( 'random_int' ) ):
return ( $min === 0 && $max === 0 ) ? random_int(0, PHP_INT_MAX) : random_int($min, $max);
elseif (function_exists('mt_rand')):
return ( $min === 0 && $max === 0 ) ? mt_rand() : mt_rand($min, $max);
endif;
return ( $min === 0 && $max === 0 ) ? rand() : rand($min, $max);
}
This may already be answered, but I wasn't getting anything in searches or in the similar questions que.
CodePudding user response:
You exceed PHP_INT_MAX. Rewrite code to use BC Math extension with arbitrary precision.
CodePudding user response:
Turns out it was pretty simple. Aside from a couple other issues, the real reason was
if ( $loc[1] < $ylimit && $loc[1] > -$ylimit ) {
which uses &&
and causes the script to continue to use the function until there are no resources left. It should be an OR operator ||