Check Percentage Chance

Written by @ 5 December 2013

This script returns a 1 (true) or 0 (false) based on the percent parameter. I use this function in any games I create to determine randomness of specific actions that would require a precise percentage.This function returns a 1 or 0. 1=true 0=false This function works with any decimal number between 0 - 100. All negatives return 0. All numbers greater than 100 return 1. A common use could be to determine a critical chance. Code is well commented. Copy and paste in an editor and run in a browser to see how it works. Took me a few days to get it running, and I tested the life out of it. Works to the nearest hundred on numbers between 0 and 100. You can tweak it to work the nearest tenth or thousandth if you like. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<?php
/* Script brought to you by: shatteredClam
	I use this function in any games I create to determine
	randomness of specific actions that would require a 
	precise percentage.
	
	This function returns a 1 or 0.
	1=true
	0=false
	
	This function works with any decimal number between 0 - 100.
	All negatives return 0.
	All numbers greater than 100 return 1.
	
	A common use could be to determine a critical chance.
	example:
	//User has a 25% chance to deal a critical hit.
	if (checkPercentageChance(25) == 1) {
		$damage = $damage * 2;
	} 
*/
function checkPercentageChance($pct) {
 //get pct to nearest hundred as integer	
 $curPct = floor(($pct * 100) + .5); 
 //set max to 10,000
 $MAX = 10000; 
 //initialize new array
 $pctArray = array();
 $num = 0;
 $flag = 0;
 
  //check for negatives
  if ($curPct <= 0) { 
    $num = 0;
  }
  //make sure not greater than 100
  else if ($curPct >= $MAX) {
  $num = 1;
  }
  //for everything in between 0-100
  else {

    for ($i = 1; $i <= $MAX; $i++) {
      //add a 1 to the array for every number up to the percent chance
	  if ($i <= $curPct) {
        $pctArray[] = 1;
      }
	  //add a 0 for the remainder of numbers in the array
      else {
        $pctArray[] = 0;
      }
    }
	//shuffle for good measure
	shuffle($pctArray);
	//generate a random number between 0 and 9,999
	//arrays start with a 0 for index..
    $rand = mt_rand(0,9999); 
	//find the 1 or 0 in the pctArray with a random index...
    $num = $pctArray[$rand];
  }

  if ($num == 1) { 
    $flag = 1;
    return $flag;
  }
  else {
    $flag = 0;
    return $flag;
  }
}

//works from 0 - 100 to the nearest hundred...
//0 = false
//1 = true
//refresh in browser to see changes..
//change numbers in ()'s to see different outcomes..

//ENJOY!!!
$zero = checkPercentageChance(0);
$hundo = checkPercentageChance(100);
$checkLow = checkPercentageChance(-20);
$checkHigh = checkPercentageChance(120);
$half = checkPercentageChance(50);
$decimals = checkPercentageChance(28.89);


echo 'test name/value on left of "="<br/>';
echo 'test with 0=false, 1=true on right of "="<br/>';
echo '<hr/>';
echo 'zero/0         = '.$zero.'<br/>';
echo 'low/-20        = '.$checkLow.'<br/>';
echo 'hundred/100    = '.$hundo.'<br/>';
echo 'high/120       = '.$checkHigh.'<br/>';
echo '<br/>';
echo 'half/50        = '.$half.'<br/>';
echo 'decimals/28.89 = '.$decimals.'<br/>';
?><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @

This user is pending a biography.

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2