[SUB] Fixed percent of bool true answers on random samples

Posted By: txesmi

[SUB] Fixed percent of bool true answers on random samples - 07/15/14 06:46

Hi,
I wrote a little module to manage the flux by a percent of success. It has no patterns or weird accumulations on a very simple process.

Code:
typedef struct 
{
	var percent;
	var balance;
} PERCBOOL;

PERCBOOL *pboolCreate ( var percent )
{
	PERCBOOL *pbool = sys_malloc ( sizeof(PERCBOOL) );
	pbool->percent = clamp ( percent, 0, 100 );
	pbool->balance = pbool->percent;
	return pbool;
}

void pboolRemove ( PERCBOOL *pbool )
{
	sys_free ( pbool );
}

BOOL pboolSample ( PERCBOOL *pbool )
{
	var percent = random ( 100 );
	if ( pbool->balance > percent )
	{
		pbool->balance -= 100 - pbool->percent;
		return TRUE;
	}
	else
	{
		pbool->balance += pbool->percent;
		return FALSE;
	}
}

void pboolModify ( PERCBOOL *pbool, var percent )
{
	pbool->percent = clamp ( percent, 0, 100 );
	pbool->balance = pbool->percent;
}



Code:
action actPlayer ()
{
   PERCBOOL pbCriticalHit;
   pboolModify ( &pbCriticalHit, 13.5 );
   ...
   if ( pboolSample ( &pbCriticalHit ) )
   ...
}



Salud!
Posted By: txesmi

Re: [SUB] Fixed percent of bool true answers on random samples - 07/15/14 16:16

Some screens.
'random(100) < percent' draws the green squares. The top number is the overall sample proportion.



Filtered with the module:



33%:



As you can see, if you take a subgroup of contiguos samples of any size in the fixed sets the ratio tends to the desired percent while mantains a random behavior. Which does not occur with the stark comparison.

Salud!
© 2024 lite-C Forums