Jump to content

thecore_random function


Recommended Posts

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I tried to translate this:

double uniform_random(double a, double 
{
	return thecore_random() / (RAND_MAX + 1.f) * (b - a) + a;
}

float gauss_random(float avg, float sigma)
{
	static bool haveNextGaussian = false;
	static float nextGaussian = 0.0f;

	if (haveNextGaussian) 
	{
		haveNextGaussian = false;
		return nextGaussian * sigma + avg;
	} 
	else 
	{
		double v1, v2, s;
		do { 
			//v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
			//v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
			v1 = uniform_random(-1.f, 1.f);
			v2 = uniform_random(-1.f, 1.f);
			s = v1 * v1 + v2 * v2;
		} while (s >= 1.f || fabs(s) < FLT_EPSILON);
		double multiplier = sqrtf(-2 * logf(s)/s);
		nextGaussian = v2 * multiplier;
		haveNextGaussian = true;
		return v1 * multiplier * sigma + avg;
	}
}

To this in php:

function uniform_random($a, $
{
	return rand() / (32767 + 1) * ($b - $a) + $a;
}
function gauss_random($avg, $sigma)
{
	$haveNextGaussian = FALSE;
	$nextGaussian = 0;

	if ($haveNextGaussian) 
	{
		$haveNextGaussian = FALSE;
		return $nextGaussian * $sigma + $avg;
	} 
	else 
	{
		do { 
			$v1 = uniform_random(-1, 1);
			$v2 = uniform_random(-1, 1);
			$s = $v1 * $v1 + $v2 * $v2;
		} while ($s >= 1 || abs($s) < 0.00001);
		$multiplier = sqrt(-2 * log($s)/$s);
		$nextGaussian = $v2 * $multiplier;
		$haveNextGaussian = TRUE;
		return $v1 * $multiplier * $sigma + $avg;
	}
}

But it don't work. Can someone help me? Is it even possible to translate it to php? 

Link to comment
Share on other sites

  • 2 weeks later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / 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.