Wrong percentage with PHP - php

$rewards = Reward::where('m_id', $module->id)->count();
$percentage = 0;
if($rewards != 0)
{
$count1 = $rewards / 100;
$count2 = $count1 * $users;
$count3 = 100 - $count2;
echo $count3;
}
$rest = 100 - $percentage;
$new = array(
'name' => $module->name,
'percentage' => $percentage,
'rest' => $rest
);
Hey,
this code above is a simple function to get percentage and then return a view with it.
The problem im having is that the $users are 569 right now and the reward is 1 so the correct percentage would be 0.569% out of 100%. But my code is return 5.69% out of 100%.
I have been searching around and i really can't find any way for noticing if this number should be with a zero or not! right now it should be 0.569% but i can't really just add a zero to it in the start as you can see.
Any solutions how to noticing if the number is only decimals or not ?

If reward is equal to one than according to your calculation count1=0.01 and count2= 5.69 .percentage calculates correctly .

Related

How to generate random numbers to produce a non-standard distributionin PHP

I've searched through a number of similar questions, but unfortunately I haven't been able to find an answer to this problem. I hope someone can point me in the right direction.
I need to come up with a PHP function which will produce a random number within a set range and mean. The range, in my case, will always be 1 to 100. The mean could be anything within the range.
For example...
r = f(x)
where...
r = the resulting random number
x = the mean
...running this function in a loop should produce random values where the average of the resulting values should be very close to x. (The more times we loop the closer we get to x)
Running the function in a loop, assuming x = 10, should produce a curve similar to this:
+
+ +
+ +
+ +
+ +
Where the curve starts at 1, peeks at 10, and ends at 100.
Unfortunately, I'm not well versed in statistics. Perhaps someone can help me word this problem correctly to find a solution?
interesting question. I'll sum it up:
We need a funcion f(x)
f returns an integer
if we run f a million times the average of the integer is x(or very close at least)
I am sure there are several approaches, but this uses the binomial distribution: http://en.wikipedia.org/wiki/Binomial_distribution
Here is the code:
function f($x){
$min = 0;
$max = 100;
$curve = 1.1;
$mean = $x;
$precision = 5; //higher is more precise but slower
$dist = array();
$lastval = $precision;
$belowsize = $mean-$min;
$abovesize = $max-$mean;
$belowfactor = pow(pow($curve,50),1/$belowsize);
$left = 0;
for($i = $min; $i< $mean; $i++){
$dist[$i] = round($lastval*$belowfactor);
$lastval = $lastval*$belowfactor;
$left += $dist[$i];
}
$dist[$mean] = round($lastval*$belowfactor);
$abovefactor = pow($left,1/$abovesize);
for($i = $mean+1; $i <= $max; $i++){
$dist[$i] = round($left-$left/$abovefactor);
$left = $left/$abovefactor;
}
$map = array();
foreach ($dist as $int => $quantity) {
for ($x = 0; $x < $quantity; $x++) {
$map[] = $int;
}
}
shuffle($map);
return current($map);
}
You can test it out like this(worked for me):
$results = array();
for($i = 0;$i<100;$i++){
$results[] = f(20);
}
$average = array_sum($results) / count($results);
echo $average;
It gives a distribution curve that looks like this:
I'm not sure if I got what you mean, even if I didn't this is still a pretty neat snippet:
<?php
function array_avg($array) { // Returns the average (mean) of the numbers in an array
return array_sum($array)/count($array);
}
function randomFromMean($x, $min = 1, $max = 100, $leniency = 3) {
/*
$x The number that you want to get close to
$min The minimum number in the range
$max Self-explanatory
$leniency How far off of $x can the result be
*/
$res = [mt_rand($min,$max)];
while (true) {
$res_avg = array_avg($res);
if ($res_avg >= ($x - $leniency) && $res_avg <= ($x + $leniency)) {
return $res;
break;
}
else if ($res_avg > $x && $res_avg < $max) {
array_push($res,mt_rand($min, $x));
}
else if ($res_avg > $min && $res_avg < $x) {
array_push($res, mt_rand($x,$max));
}
}
}
$res = randomFromMean(22); // This function returns an array of random numbers that have a mean close to the first param.
?>
If you then var_dump($res), You get something like this:
array (size=4)
0 => int 18
1 => int 54
2 => int 22
3 => int 4
EDIT: Using a low value for $leniency (like 1 or 2) will result in huge arrays, since testing, I recommend a leniency of around 3.

PHP calculate percentages

I need some help. It's a simple code, but I don't have idea how to write in down. I have the numbers:
$NumberOne = 500;
$NumberTwo = 430;
$NumberThree = 150;
$NumberFour = 30;
At all this is:
$Everything = 1110; // all added
Now I want to show what percentage is for example $NumberFour of everything or what percentage is $NumberTwo of $Everything. So the "market share".
Use some simple maths: divide the number you wish to find the percentage for by the total and multiply by 100.
Example:
$total = 250;
$portion = 50;
$percentage = ($portion / $total) * 100; // 20
Solution to original example
To get $NumberFour as a percentage of the total amount you'd use:
$percentage = ($NumberFour / $Everything) * 100;
Rounding
Depending on the numbers you're working with, you may want to round the resulting percentage. In my initial example, we get 20% which is a nice round number. The original question however uses 1110 as the total and 30 as the number to calculate the percentage for (2.70270...).
PHP's built in round() function could be useful when working with percentages for display: https://www.php.net/manual/en/function.round.php
echo round($percentage, 2) . '%'; // 2.7% -- (30 / 1110) * 100 rounded to 2dp
Helper Functions
I would only contemplate creating helper functions when their use justifies it (if calculating and displaying a percentage isn't a one-off). I've attached an example below tying together everything from above.
function format_percentage($percentage, $precision = 2) {
return round($percentage, $precision) . '%';
}
function calculate_percentage($number, $total) {
// Can't divide by zero so let's catch that early.
if ($total == 0) {
return 0;
}
return ($number / $total) * 100;
}
function calculate_percentage_for_display($number, $total) {
return format_percentage(calculate_percentage($number, $total));
}
echo calculate_percentage_for_display(50, 250); // 20%
echo calculate_percentage_for_display(30, 1110); // 2.7%
echo calculate_percentage_for_display(75, 190); // 39.47%
Create function to calculate percentage between two numbers.
<?php
/**
* Calculate percetage between the numbers
*/
function percentageOf( $number, $everything, $decimals = 2 ){
return round( $number / $everything * 100, $decimals );
}
$numbers = array( 500, 430, 150, 30 );
$everything = array_sum( $numbers );
echo 'First of everything: '.percentageOf( $numbers[0], $everything )."%\n";
echo 'Second of everything: '.percentageOf( $numbers[1], $everything )."%\n";
echo 'Third of everything: '.percentageOf( $numbers[2], $everything )."%\n";
echo 'Fourth of everything: '.percentageOf( $numbers[3], $everything )."%\n";
?>
This outputs
First of everything: 45.05%
Second of everything: 38.74%
Third of everything: 13.51%
Fourth of everything: 2.7%
You can also come up with the percentage with the following formula. For the percentage, add a 0. in front of it. So 5% would be 0.05. Total X 0.05 is the amount.

Defining percentage for random number

My rand(0,1) php function returns me the 0 and 1 randomly when I call it.
Can I define something in php, so that it makes 30% numbers will be 0 and 70% numbers will be 1 for the random calls? Does php have any built in function for this?
Sure.
$rand = (float)rand()/(float)getrandmax();
if ($rand < 0.3)
$result = 0;
else
$result = 1;
You can deal with arbitrary results and weights, too.
$weights = array(0 => 0.3, 1 => 0.2, 2 => 0.5);
$rand = (float)rand()/(float)getrandmax();
foreach ($weights as $value => $weight) {
if ($rand < $weight) {
$result = $value;
break;
}
$rand -= $weight;
}
You can do something like this:
$rand = (rand(0,9) > 6 ? 1 : 0)
rand(0,9) will produce a random number between 0 and 9, and whenever that randomly generated number is greater than 6 (which should be nearly 70% time), it will give you 1 otherwise 0...
Obviously, it seems to be the easiest solution to me, but definitely, it wont give you 1 exactly 70% times, but should be quite near to do that, if done correctly.
But, I doubt that any solution based on rand will give you 1 exactly 70% times...
Generate a new random value between 1 and 100. If the value falls below 30, then use 0, and 1 otherwise:
$probability = rand(1, 100);
if ($probability < 30) {
echo 0;
} else {
echo 1;
}
To test this theory, consider the following loop:
$arr = array();
for ($i=0; $i < 10000; $i++) {
$rand = rand(0, 1);
$probability = rand(1, 100);
if ($probability < 30) {
$arr[] = 0;
} else {
$arr[] = 1;
}
}
$c = array_count_values($arr);
echo "0 = " . $c['0'] / 10000 * 100;
echo "1 = " . $c['1'] / 10000 * 100;
Output:
0 = 29.33
1 = 70.67
Create an array with 70% 1 and 30% 0s. Then random sort it. Then start picking numbers from the beginning of the array to the end :)
$num_array = array();
for($i = 0; $i < 3; $i++) $num_array[$i] = 0;
for($i = 0; $i < 7; $i++) $num_array[$i] = 1;
shuffle($num_array);
Pros:
You'll get exactly 30% 0 and 70% 1 for any such array.
Cons: Might take longer computation time than a rand() only solution to create the initial array.
I searched for an answer to my question and this was the topic I found.
But it didn't answered my question, so I had to figure it out myself, and I did :).
I figured out that maybe this will help someone else as well.
It's regarding what you asked, but for more usage.
Basically, I use it as a "power" calculator for a random generated item (let's say a weapon). The item has a "min power" and a "max power" value in the db. And I wanted to have 80% chances to have the "power" value closer to the lower 80% of the max possible power for the item, and 20% for the highest 20% possible max power (that are stored in the db).
So, to do this I did the following:
$min = 1; // this value is normally taken from the db
$max = 30; // this value is normally taken from the db
$total_possibilities = ($max - $min) + 1;
$rand = random_int(1, 100);
if ($rand <= 80) { // 80% chances
$new_max = $max - ($total_possibilities * 0.20); // remove 20% from the max value, so you can get a number only from the lowest 80%
$new_rand = random_int($min, $new_max);
} elseif ($rand <= 100) { // 20% chances
$new_min = $min + ($total_possibilities * 0.80); // add 80% for the min value, so you can get a number only from the highest 20%
$new_rand = random_int($new_min, $max);
}
echo $new_rand; // this will be the final item power
The only problem you can have, is if the initial $min and $max variables are the same (or obviously, if the $max is bigger than the $min). This will throw an error since the random works like this ($min, $max), not the other way around.
This code can be very easily changed to have more percentages for different purposes, instead of 80% and 20% to put 40%, 40% and 20% (or whatever you need). I think the code is pretty much easy to read and understand.
Sorry if this is not helpful, but I hope it is :).
It can't do any harm either way ;).

Calculating Highly divisible triangular number with PHP

I am trying to resolve project euler problem no 12 with PHP but it is taking too much time to process. I came across with similar processing problems of PHP while solving previous problems and I had to solve them in C++ just to test whether my approach is correct or not.
I want to know whether there is something wrong with my approach or somehow I can do something to make processing fast. Here is the code of my solution which works well for the triangle having 360 divisors. The link of problem is http://projecteuler.net/problem=12 and here is my code
<?php
set_time_limit(0);
ini_set('memory_limit', '1G');
$triangles = array(0);
$count = 1;
$numOfDivisiors = 0;
$lastTriangle = 0;
while($numOfDivisiors < 500){
$triangle = (int) $lastTriangle + (int) $count;
$factors = getFactors($triangle);
//$triangles[] = array('triangle' => $triangle, 'factors' => $factors, 'factorsCount' => count($factors));
$triangles[] = array('triangle' => $triangle, 'factorsCount' => count($factors));
$lastTriangle = $triangle;
$numOfDivisiors = count($factors);
$count++;
//echo '<pre>'; print_r(array('triangle' => $triangle, 'factorsCount' => count($factors), 'count' => $count)); echo '</pre>';
}
echo $numOfDivisiors; exit;
/**
for($i = 0 ; $i < 10 ; $i++){
}
**/
//echo '<pre>'; print_r($triangles); exit;
function getFactors($number){
$factors = array();
$break = false;
$count = 1;
while($break != true){
$remainder = $number % $count;
if($remainder == 0){
$factors[] = $count;
}
//echo $count." ".$number; exit;
if($count == $number){
$break = true;
}
$count++;
}
return $factors;
}
?>
use some maths.
triangle numbers can be generated by
n(n+1) /2
and that if you can find the prime factors, Adding 1 to their powers and multiplying together gives the number of divisors.
My PHP solution takes around 4 seconds and i think i can speed that up also
There are several ways to speed up your solution. The first one I'd point you at is the following:
if a * b = c then both a and b are factors of c
One of a and b will be <= to the square root of c
this should help speed up your solution

Efficient Leveling System

I'm making a leveling system based on experience you have on the site. I already have all the experience stuff figured out, and how I want to do the leveling, but I need a more efficient way to do it. I know this would probably would be achieved using an array, but I don't really know how to go about doing that. Enough blabbering, though, this is what I'm trying to do...
Level 1 will be anything under 150 experience
Then I'm going to multiply that by 1.5, so
Level 2 will be anything under 225
Level 3 will be anything under 337.5 and so on. This is the inefficient way that I was going to do.
if($xp < 150){
$level = "1";
}elseif($xp < 225){
$level = "2";
}elseif($xp < 337.5){
$level = "3";
}
I could use a variable for the number and multiple by 1.5 ($number*1.5), but like I said before I don't really know how that'd work.
*Some more info..
I have a session file included on every page and I have queries that would check every time there is new experience earned and this code would be used to update the level on the database automatically.
Try
$level = min(max((int)(log($xp / 100, 1.5) + 1), 1), $maxLevel);
That takes the logarithm base 1.5 of xp/100 (i.e. the number of times you'd have to multiply 100 by to get $xp), then adds one (since log($x, 1.5) is less than one if $x is less than 1.5). The min(max(..., minLevel), maxLevel) construct lets you clamp the level to lie between 1 and $maxLevel, also avoiding any issues with negative levels (if $xp is sufficiently less than 150).
Here's how I did my level system. It's a little more advanced featuring skill points..
<?php
$level_up = ($level + 1);
if ($exp >= $max_exp)
{
$sql = "UPDATE users SET level=(level + 1) , max_exp=(exp * 1.05) , skill_points=(skill_points + 3) WHERE id='".$id."' LIMIT 1";
$res = mysql_query($sql);
if ($exp >= $max_exp)
echo '<div class="Leveled">' . 'You sucessfully leveled up to ' . $level_up . '!' . ' As a reward you were given 3 skill points!' . '</div>';
}
else
{
}
?>
I'd agree. either objects or arrays would be best.
Something like:
$array = array(
array(
'xp_required' => 150,
'level' => 1
),
array(
'xp_required' => 225,
'level' => 2
),
array(
'xp_required' => 337.5,
'level' => 3
)
);
$current_xp = 155;
foreach( $array as $reqs ){
if( $current_xp > $reqs['xp_required'] ){
$level = $reqs['level'];
}
}
echo $level';
well at the moment once you grab the users exp you can have that code block as a function to constantly check for variations in exp levels, but use a ranged statement
if($xp < 150){
$level = "1";
}elseif($xp > 150 && $xp < 225 ){
$level = "2";
}elseif($xp > 225 && $xp < 337.5){
$level = "3";
}

Categories