Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If i have a loop for example
for ($a=1; $a<6; $a++)
Is it possible to say for the first loop if another variable like $b is the same as one of the other numbers that will come like 2, 3, 4 or 5.
And what if i have a word split, so every letter is a variable like this:
$letter1 = str_split($word)[0];
$letter2 = str_split($word)[1];
$letter3 = str_split($word)[2];
$letter4 = str_split($word)[3];
$letter5 = str_split($word)[4];
How can i check if $a is at 1 in the loop,if it is not letter1 but letter2, 3, 4 or 5?
sure. Just check it against the counter:
for ($a=1; $a<6; $a++){
if ($a===$b){
echo ("b is caught!");
break;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a math function, for example:
string = "3x+6.5y-23z"
I need to extract the function and get x, y, z; give value x = 6, y = 7, z = 8; and solve.
you can do it like this
<?php
$x = 6;
$y = 7;
$z = 8;
echo $string = 3*$x+6.5*$y-23*$z;
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Want to divide random integer number into equal five parts. And insert those values into array. Can any one tell me the logic for that.
Eg. I have 15 as my number. After dividing. It should generate the array as below.
$myArray = array('3','6','9','12','15');
Thanks in advance.
function getParts($number, $parts)
{
return array_map('round', array_slice(range(0, $number, $number / $parts), 1));
}
print_r(getParts(15, 5));
Explanation: range() generates the array of values starting with 0, ending when it reaches $number and using the step $number/$parts. It will get $parts+1 floating point numbers. array_slice() removes the first item (which is always 0). array_map() applies the function round() to each element to get the nearest integer.
Divide and create a loop to fill the array...
$total = 15;
$divide = 5;
$base = $total / $divide;
$arr = array();
for($i = 1; $i <= $divide; $i++) {
$arr[] = round($i * $base);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have long decimal numbers that I'd like to make easier to read. There's many 0's involved and I would like to bold the end of the string from where the numbers stop being just 0's. The number will always have 8 decimal places, no more, no less. It's worth noting that I would like any trailing zeros to also be bold, I don't just want to avoid bolding 0 as a whole.
Example:
Before - 0.00004320
After - 0.00004320
Any help or pointers in the right direction much appreciated.
Thanks.
You can do it using a loop and some string manipulation:
function doTheThing($number) {
$length = strlen($number);
$period = strpos($number, '.') + 1;
for ($i = $period; $i < $length; $i++) {
if ($number[$i] !== '0') {
$number = substr($number, 0, $i).'<b>'.substr($number, $i).'</b>';
break;
}
}
return $number;
}
echo doTheThing('0.00004320');
Or you can do it using a regular expression replacement:
function doTheThing($number) {
return preg_replace('/^(\d+\.0*)(\d+)$/', '$1<b>$2</b>', $number);
}
echo doTheThing('0.00004320');
echo doTheThing(sprintf('%.8f', 0.00004320)); // if your number is not a string
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The code below works, but I need to run a loop for the results. For example where it says $results0 I need it to say $results[$var].
for($i = 0; $i < (count($results0)); $i++) {
$teams[$i] = array(
$results0[$i],
$results1[$i],
$results2[$i],
$results3[$i],
$results4[$i],
);
}
$teams1 = array_sort($teams, $sortVar, SORT_ASC);
I realize you cannot do this but I need something that looks like this but actually works:
for($i = 0; $i < (count($results0)); $i++) {
$teams[$i] = array(
for($j = 0; $j < (count($teams)); $j++) {
${'results'.$j}[$i],
);
}
}
$teams1 = array_sort($teams, $sortVar, SORT_ASC);
Also now I need to be able to sort by category. Thanks in advance, I'm aware my current code may not be secure, I'm doing that after I'm finished.
You might simply use array_map():
$results1 = array(1, 2, 3);
$results2 = array('a', 'b', 'c');
$results3 = array('I', 'II', 'III');
$zipped = array_map(function($r1, $r2, $r3) {
return array($r1, $r2, $r3);
}, $results1, $results2, $results3);
var_dump($zipped);
We unlikely are able to provide an answer to the question about sorting since we have no idea what is "category" in your code. It doesn't appear anywhere. But give usort() a try.