Calculate average time difference between multiple unix timestamps - php

i feel a little bit stupid, but let's imagine, that i have a set of unix timestamps:
1375110404
1374660925
1374482694
1374242337
1373793867
1373632889
1373187141
1373021668
1372754021
1372599890
What i'm trying to achieve is simple: I just want to calculate the average time difference between these 10 timestamps. I just can't find the proper way for the calculation.
What i just tried was
1375110404 - 1374660925 = 449479
1374482694 - 1374242337 = 240357
1373793867 - 1373632889 = 160978
1373187141 - 1373021668 = 165473
1372754021 - 1372599890 = 154131
449479 + 240357 + 160978 + 165473 + 154131 = 1170418
1170418 / 5 = 234083,6
but that looks illogical to me. Any advice is greatly appreciated.
EDIT:
All these stamps come from a php array.
EDIT:
Thanks to Orangepill for pointing me to the right direction. Here's the final solution:
for($cnt = count($array), $res = 0, $i = 1; $i < $cnt; $i++) {
$res += $array[$i-1] - $array[$i];
}
echo $res/$cnt;
This calculates
1375110404 - 1374660925 = 449479
1374660925 - 1374482694 = 178231
1374482694 - 1374242337 = 240357
1374242337 - 1373793867 = 448470
1373793867 - 1373632889 = 160978
1373632889 - 1373187141 = 445748
1373187141 - 1373021668 = 165473
1373021668 - 1372754021 = 267647
1372754021 - 1372599890 = 154131
449479 + 178231 + 240357 + 448470 + 160978 + 445748 + 165473 + 267647 + 154131 = 2510514
2510514 / 10 = 251051.4
which looks correct to me.

The most straight forward way it to do it like you described.
$res =0;
for($x = 1, $num = count($array); $x < $num; $x++){
$res =+ $array[$x] - $array[$x-1];
}
echo $res/($num-1);

The current accepted answer will give incorrect results if the the timestamps are not in strict chronological order. That is, negative values will skew your average. I may be wrong, but I imagine that you don't want to count any time difference as a negative value, after all, you can't run 100m in -12 seconds!
I provide this answer as an alternative that will always give an average based on positive time differences regardless of the order of the times in the passed array:-
function array_average_diff(array $array)
{
$diff = 0;
for($i = 1; $i < count($array); $i++){
$diff += abs($array[$i] - $array[$i - 1]);
}
return $diff/count($array);
}
See it working

Related

Bailey-Borwein-Plouffe in php trouble

I am trying to implement the BBP algorithm in php. My code is returning a decimal which i thought was odd as it should be in hex. I was told to convert to decimal from hex by multiplying by 16 also but now its all just wrong. Here is a sample:
$n1=$n2=$n3=$n4=$n5=$n6=$n7=$n8 =0;
$S1=$S2=$S3=$S4=$S5=$S6=$S7=$S8 = 0; //initializing
$k = 0;
$m1= 8*$k + 1;
$m2 = 8*$k + 4;
$m3 = 8*$k + 5;
$m4 = 8*$k = 6;
$b =16;
$e=$n-$k;
while($k<$n){ //Sum 1 of 8
$S1 +=Modular($b, $m1, $e)/$m1; //see Moduler_Expansion.php
$k++;
}
$k = $n +1; //redefine for second sum, and every other
while($k<$limit){ //Sum 2 of 8
$S2 += (pow($b,$n-$k))/($m1);
$k++; //now repeat similar process for each sum.
}
and I repeat the process for each term of BBP then:
$S = 4*($S1 + $S2) - 2*($S3+$S4) -($S5+$S6) - ($S7+$S8);
`
Following the wiki page I then strip the integer and multiply by 16, but for $k =0 I get; 3.4977777777778
and for $k = 1: 7.9644444444448.
I dont think these are right, it could just be i do not know how to interpret th ouput properly. Can anyone offer any advice?

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 infinity loop [While]

I am having some problems with PHP.
I used while to sum a number's digits always that it has more than two digits, some how, it gets into an infinity loop.
e.g: 56 = 5 + 6 = 11 = 1+1= 2.
Here is the code:
$somaP = 0;
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Can anyone help me? Guess it is a simple mistake, but I couldn't fix it.
You need to reset the value of $somaP in your while loop.
Currently it continues to increase its value every time through the loop.
Try this:
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
$somaP = 0;
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Take a look at this line:
$numPer = (string) $somaP;
It seems that the length of $somaP is never lesser (or equal) than 1. So the length of $numPer is never lesser (or equal) than 1.
What are you trying to do?
It's unclear to me.
This for example would add every number in a string together?
E.g "1234" = 1+2+3+4 = 10
$total = 0;
for($i = 0; i < strlen($string); $i++){
$total += $string[$i];
}
echo $total;
This looks cleaner I would say:
$numPer = 56;
while ($numPer > 9){
$numPer = array_sum(str_split($numPer));
}
echo $numPer;
PHP handles all string <> number conversions for you, so no need to do (string) on a number unless really needed.

Find the missing number in the given array of numbers using PHP

Am new to php i have faced an interview some days ago, and the interviewer asked a question like the following one.
The given array has 99 numbers, which contains the digits from 1 to 100
with one digit missing. Describe two different algorithms that finds you the missing number. The algorithm should optimize for low storage and fast processing. Output should show the execution time of each algorithm.
And i have searched google about it, and come to know its a common puzzle used to ask in interviews. I found out the answer like this way.
int sum = 0;
int idx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
idx = i;
} else {
sum += arr[i];
}
}
// the total sum of numbers between 1 and arr.length.
int total = (arr.length + 1) * arr.length / 2;
System.out.println("missing number is: " + (total - sum) + " at index " + idx);
But the code is not in php,
Can u please help me to find out the php code and algorithm name. So i can improve my answer in the next interviews.
In PHP you can easily use some array functions and achieve that. Best way is,
$missing = array_diff(range(1,100),$array);
DEMO.
Another way to do it is by using the array_sum function and the knowledge that all numbers from 1 to 100 added together equals 5050.
$missing = 5050-array_sum($array);
Converted to PHP, it is nearly the same.
(Not tested)
Of course, there are better ways, like the one Rikesh posted, but this is the exact one you asked for:
$sum = 0
$idx = -1
for($i = 0; $i < count($arr); $i++){
if($arr[$i] == 0){
$idx = $i;
}else{
$sum += $arr[$i];
}
}
$total = (count($arr) + 1) * (count($arr) / 2);
echo "Missing: " . ($total - $sum) . " at index " . $idx;
$arr=range(1,99);
$j=1;
for($i=0;$i<100;$i++){
if(!in_array($j,$arr)){
echo $j.'is missing';
}
$j++;
}

How to get the greatest difference among a list of ordered numbers in PHP?

For example,
1,3,6,8,11,45,99
The interval between numbers is:
2,3,2,3,34,54
So the greatest difference is 54.
How to implement this function?
function get_greatest_diff($arr_of_numbers)
{}
You got a lot of different options:
Sort the array, then compare the first and the last element
For each element, compare it to each subsequent element. Keep the highest difference in memory.
Implement some kind of merge-sort, but return the difference instead of the original sorted values.
You should handle the case where the array has less than 2 elements separately:
$maxDiff = -1;
for ($i = 0; $i + 1 < count($array); $i++) {
$diff = $array[$i + 1] - $array[$i];
if ($diff > $maxDiff)
$maxDiff = $diff;
}
}
You should do something like this :
$greatest_diff = 0;
for($i = 0; $i < sizeof($arr_of_numbers) - 1; $i++)
{
$current_diff = $arr_of_numbers[$i + 1] - $arr_of_numbers[$i];
if($current_diff > $greatest_diff){
$greatest_diff = $current_diff;
}
}
echo $greatest_diff;

Categories