In PHP, I am getting a range like this...
$number = range(0,50,10);
This is working, but now I am trying to modify it so that given a number, it will get the range 5 digits either side of that number.
So, for example, given the number 25, I would like...
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
To further slightly complicate things, I would only like these numbers to be positive, so if the start number was 3, then it would only get the range 1-8
Just subtract and add to the number in the center. Use the max() function to restrict the beginning to at least 1.
$n = 25;
$numbers = range(max(1, $n-5), $n+5)
max() will help you out here.
$width = 5; // Width of your range
$center = 3; // Center of your range
$r = range(max(1, $center-$width), $center+$width);
You could try something like this:
for ($i = $number - 5; $i <= $number + 5; $i++) {
if ($i > 0) {
return $i;
} else {
return "negative";
}
}
I am new to PHP and not entirely sure if that will work 😬
Related
Lets say for $numArr = array(10,20,30,40,50,60,70,80,90,100);
how to sum between specific range like 30 - 60 to get sum of 180.. (30+40+50+60).
edit : This is my latest code
<?php
function sum_array ($no1, $no2){
$array = array(10,20,30,40,50,60,70,80,90,100);
$input = array_slice($array, $no1, $no2);
return array_sum($input);
}
echo sum_array(0,3);
?>
I made a basic function for this according to u guys replies .. though still i want to put some validations into this like the parameters should be positive else the function should return -1 .. and what if the second index of the range is not in array.. like (90-120). Would it able to still sum what's in range and in array .. and give 190 to the above range.
$numArr = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
$startValue = 30;
$endValue = 60;
$startIndex = array_search($startValue, $numArr);
$endIndex = array_search($endValue, $numArr);
$length = $endIndex - $startIndex + 1;
$result = array_sum(array_slice($numArr, $startIndex, $length));
print_r($result);
I need to get the greatest even number out of this array with a for-loop. I know how to get the highest number from the loop, but it's not even.
This is the code I have so far:
<?php
// array aangemaakt
$aReeks = array(23, 245, 1, 2, 12, -10, 46, 6, 66, 9999, -55, 348, 56, 6, 66, 983);
$resultaat = 0;
for ($i = 0; $i < count($aReeks); $i++) {
if ($resultaat < $aReeks[$i])
$resultaat = $aReeks[$i];
}
echo $resultaat;
?>
if($resultaat < $aReeks[$i] && $aReeks[$i] % 2 == 0)
modulo of division by two is zero -> even number
To find the highest odd number inside an array, you can use array_filter and max.
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
echo(max(array_filter($aReeks, function($var){return(!($var & 1));})));
//348
If you just need to find if a number is even or odd, you can use:
//if Even Number
$number = "222";
if(!($number & 1)){...}
//if Odd Number
$number = "221";
if($number & 1){...}
I need to distribute a number for multiple points in degraded order.
For example,
N = 100 // this 100 is fixed
no_of_points = 10 // no of points will vary
Need to break 100 into 10 points in descending order RANDOMLY.
e.g:
80, 55, 32, 18, 10, 10, 10, 8, 2, 0
Here,
degradation will be faster towards end
Must contain a zero to end
I'm trying to do something like:
private static function generateRandomPercentage($no_of_points)
{
$distributions = [];
// need to start from 80
// but it may vary also
$max = mt_rand(80, 81);
$ratio = $max / $no_of_points;
for( $i = 1; $i <= $no_of_points; $i++ ) {
$delta = ($ratio * $i);
$distributions[] = round(($max - $delta) / 100, 2);
}
print_r($distributions);
}
But nothing working.
Please help me.
Can be done with php standard functions:
$N = 100 // this 100 is fixed
$no_of_points = 10 // no of points will vary
$distributions= rsort(array_slice(shuffle(range(1,$N)), 0, $no_of_points);
print_r($distributions));
range = create array with values 1...n
shuffle = shuffle array
array_slice = return part of an array
rsort = sort array values hight to low values
So I need to make unique purchase number of mobile game. Each user's buying of in-app-purchase item record should be attached by unique long type 19 digits numbers from php code and stored to mysql DB.
But how can I actually make random 19 digits numbers?
I tried like this but errors.
$num_str = sprintf("%19d", mt_rand(1, 9999999999999999999));
you can also try this:
function random19() {
$number = "";
for($i=0; $i<19; $i++) {
$min = ($i == 0) ? 1:0;
$number .= mt_rand($min,9);
}
return $number;
}
echo random19();
which would output some random 19 numbers: 6416113158912395605
You have to do something like -
$num_str = sprintf("%10d", mt_rand(1, mt_getrandmax())).sprintf("%9d", mt_rand(1, mt_getrandmax()));
if (strlen($num_str) < 19)
$num_str = str_pad($num_str, 19, rand(0, 9), STR_PAD_RIGHT);
else if(strlen($num_str) > 19)
$num_str = substr($num_str, 0, 19);
echo $num_str;
The distribution of mt_rand() return values is biased towards even numbers on 64-bit builds of PHP when max is beyond 2^32. This is because if max is greater than the value returned by mt_getrandmax(), the output of the random number generator must be scaled up.
$randNumber = date("YmdHis").rand(11111,99999);
Apologies for the confusing title, I couldn't phrase it much differently.
I want to be able to generate a random range of x number(s) within the number 10000.
Such as 200-500 (201, 202, 203... ...499) or 9700 to 10000.
I also want the function to be easily changed so it will be a range of x-amount numbers. The above was a range of 200, x = 200... I would like to be able to easily change that.
Thank you.
Makes use of range():
$min = 1;
$max = 10000;
$nb = 300;
// in this example, $start can be from 1 to 9701
$start = mt_rand($min, $max - $nb + 1);
// in this example, $result can be from [1;300] to [9701;10000]
$result = range($start, $start + $nb - 1);
Try mt_rand()
echo mt_rand(0, 10000);