Math returns wrong answers [duplicate] - php

This question already has answers here:
Random Float between 0 and 1 in PHP
(13 answers)
Closed 8 years ago.
I've been trying to get this work for almost an hour now, and it's constantly eluding me.
public static function npcBattleStats($npc) {
$npc = self::findNPC($npc);
$rand = mt_rand(1.2, 3.3);
$npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);
$npcSpeeda = round($npc['power'] / 2.4);
$npcSpeedb = round(($npc['power'] / 1.1) + 2);
$npcStats['speed'] = mt_rand($npcSpeeda, $npcSpeedb);
return $npcStats;
}
level = 3 | power = 50
$npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);
Everything else seems to work properly, but the attack is constantly off. It should be between 45 and 115 but only returns the following.
[attack] => 50 | [attack] => 150 | [attack] => 75
I hope I'm not missing something simple here, but help would be greatly appreciated.

It seems that the Funktion mt_rand() only spits aut integers, not floats. Try this instead:
$rand = mt_rand(12, 33) / 10;

First of all as cHao mentioned
PHP manual: "Return value: A random integer value between min (or 0) and max (or mt_getrandmax(), inclusive), or FALSE if max is less than min."
To get it fixed you need something like this
$rand = mt_rand(35, 100) / 10;
//35 is your max
//120 is your low
So when You get:
your max:
(5 * 50) / 3.5 = 42.85.. //so round it
your low:
(5 * 50) / 10 = 15

Related

How to convert numbers like 902.1 or 902 to 900 in php [duplicate]

This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 3 years ago.
Anyone knows how i can convert How to convert numbers like 902.1 or 902 to 900 in php..i have tried both ceil and floor like below but still same issue
$amount = 902.1
$amount2 = floor($amount);
return $amount2;
returns 902
but i want to return either 900 or 910..something like this
This did it for me
$number = ceil($amount / 10) * 10;

How can I round down to the nearest multiple of 3 in PHP? [duplicate]

This question already has answers here:
Best way to round down in PHP
(3 answers)
Round up to nearest multiple of five in PHP
(12 answers)
Closed 4 years ago.
How can I round an integer down to the nearest multiple of 3 using PHP? And have <3 be 0.
For example:
4 becomes 3
10 becomes 9
9 becomes 9
8 becomes 6
And so on...
Assuming $x is your input:
print $x-$x%3;
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* #param integer $number
* #param integer $multiple
* #return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
I know there are good answers here but this one is for larger numbers for the sake of alternative, using bcmath.
function floor_to_multiple($number, $multiplier) {
return bcsub($number, bcmod($number, $multiplier));
}
If you want for 3, you may use function below:
function round_nearest_3($value){
return $value-$value%3;
}
If you want to return function for any value use function below:
function round_nearest_mod($value,$mod){
return $value-$value%$mod;
}
Examples:
echo round_nearest_3(4); // becomes 3
echo round_nearest_3(10); // becomes 9
echo round_nearest_3(9); // becomes 9
echo round_nearest_3(8); // becomes 6
echo round_nearest_mod(4,3); // becomes 3
echo round_nearest_mod(10,3); // becomes 9
echo round_nearest_mod(9,3); // becomes 9
echo round_nearest_mod(8,3); // becomes 6
<?php
//requested number
$num = 10;
//calc
for($i=1;($i*3)<=$num;$i++)$answer[] = $i;
$answer = max($answer)*3;
//print result
print_r($answer);
I misread that even if it is a perfect fit, it should round down to the last preceeding incremental:
4 becomes 3
10 becomes 9
9 becomes 6
8 becomes 6
Therefore, if for some reason you need this; your answer would be:
print $x-($x-1)%3-1;
I made a mistake in comprehending the question but thought this answer was curious enough to be worth posting.

Understanding something more about the % Modulus operator

I am learning to work with some math like PHP query and just got to the modulo, I am not quite sure in what situations to use this because of something i stumbled on and yes I did already read one of the posts here about the modulo :
Understanding The Modulus Operator %
(This explanation is only for positive numbers since it depends on the language otherwise)
The quote above is in the top answer there. But if I focus on PHP only and i use the modulo like this:
$x = 8;
$y = 10;
$z = $x % $y;
echo $z; // this outputs 8 and I semi know why.
Calculation: (8/10) 0 //times does 10 fit in 8.
0 * 10 = 0 //So this is the number that has to be taken off of the 8
8 - 0 = 8 //<-- answer
Calculation 2: (3.2/2.4) 1 //times does this fit
1 * 2.4 = 2.4 //So this is the number that has to be taken off of the 3.2
3.2 - 2.4 = 0.8 // but returns 1?
So my question is why does this exactly happen. my guess would be that in the first phase it would get 8/10 = 0,8 but this doesn't happen. So can someone explain a bit about why this happens. I understand the modulo's basics like if I do 10 % 8 = 2 and I semi understand why it doesn't return something like this: 8 % 10 = -2.
Also, is there a way to modify how the modulo works? so it would return a - value or a decimal value in the calculation? or would I need to use something else for this
Little shortened: why does this happen when I get a negative number in return and is there some other way or operator that can actually do the same and get in the negative numbers.
Modulus (%) only works for integers, so your calculation at the bottom of your example is correct...
8/10 = 0 ( integer only ), remainder = 8-(0*10) = 8.
If you instead had -ve 12 - -12%10...
-12/10 = -1 (again integer only), remainder = -12 - (10*-1) = -2
For floats - you can use fmod(http://php.net/manual/en/function.fmod.php)
<?php
$x = 5.7;
$y = 1.3;
$r = fmod($x, $y);
// $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7
(Example from manual)

Rounding numbers to ten [duplicate]

This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 8 years ago.
What I'm trying to do seems rather simple, but I can't get it to work with the php.net documentation..
Numbers should always be rounded to "tens"
So:
1 -> 10
7 -> 10
12 -> 20
18 -> 20
23 -> 30
35 -> 40
Something like this should work:
$rounded_num = round($orig_num / 10) * 10;
Just read documentation about round.
round($number, -1);
Your should use round() function.
But if you want manual way, you can use something like that:
$number = 189;
$length = strlen((string)$number);
$div = '1';
for ($i=0;$i<$length-1;$i++){
$div .= '0';
}
$result = (int)$div*(ceil($number/$div));
$result = 200

PHP math question [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?
Here is the code.
$percent = ($avg / 15) * 100;
The sprintf function is made for this (see also the manual):
echo sprintf('%.1f%%', $percent);
PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:
$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%

Categories