Jump to content
Forgot your email address? ×
×
×
  • Create New...
  • 0

thecore_random function


Pumarolonius

Question

  • Answers 3
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

3 answers to this question

Recommended Posts

  • 0

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Announcements

  • -15% Discount in Metin2 Dev Store (01/10/2021 => 31/12/2021) => Code => 15_PCT_OCT_TO_DEC_2021


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.