PHP Weighted random number - php

How can I generate a weighted random number between 1 and 10 with 10 being the highest chance and 1 being the lowest chance?
rand(1,10) ?
Needs to be a simple one line code since it will be run 100,000's of times

OK I think I understand what you're trying to say..
try this :
mt_rand(mt_rand(1, 10),10 );
I looped it a million times :
10 = 292634
9 = 193333
8 = 142815
7 = 109580
6 = 84616
5 = 64498
4 = 47666
3 = 33450
2 = 21286
1 = 10122

Related

I want to print numbers pattern program but it not give me wrong out put

I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP

Bit integer value from numerated list

How would one get the integer value of the n-th value from n?
This is hard to phrase so I'll just use English. If I wanted the 3rd integer value from 1...
1 = 1
2 = 3
3 = 4 <- (looking to get 4 using 3)
4 = 8
5 = 16 <- (or 16 using 5)
...
I could just do a lookup table, but I'm sure there's a better solution.
$bitvalue = 5;
$intvalue = 2 ** ($bitvalue - 1);
// gives 16
echo $intvalue;
The ** operator is the power operator. So I'm using powers of 2.

7001 write 8? round(7001/1000)

I have to write numbers in groups of 1000.
so, if I have 7000, I want to write:
1 2 3 4 5 6 7
but If I have 7001 or more, I want to write:
1 2 3 4 5 6 7 8
php
$num = 22501;
$num = round($num/1000);
for ($i=1; $i<=$num; $i++){
echo $i;
}
if my number is 22501 I will write 1 to 23. but if it is 22001 it will write 1 to 22.
I want to write 1 to 23. how can I do this?

How to calculate personal number by date of birth using php

I want to make the calculation of personal number by date of birth.
The calculation is done in this manner:
Ex. 8 (day) +12 (month) + 1 + 9 + 7 + 1 (year) = 38 = 3 + 8 = 11 = 1 + 1 = 2
(the final number)
This final number must not be greater than nine.
So:
The first number comes is 38 greater than 9 and it should make 3 + 8
The second number comes is 11 greater than 9 it should make 1 + 1
The third number comes is 2 less than 9 so it is the final number.
Taking all these calculations should let out the number 2.
How can I get it with php calculation?
I suppose, you can split date to array. Then
$arr = array(8,12,1,9,7,1);
// sum array, split sum to array per digit untill more than 1 digit in sum
while (count($arr = str_split(array_sum($arr))) != 1) {}
echo $arr[0]; // 2

PHP round up list items to nearest five

I've got a grid of 10 square list items. A bit like a gallery. If the user adds another item there will be 11. However this will look strange as the 11th item will be on its own in a new row. How can I use PHP to round up to the nearest 5 and add in the some blank/dummy list items?
You could use the modulo operator to identify the remainder of a division:
10 % 5 = 0
11 % 5 = 1
12 % 5 = 2
13 % 5 = 3
14 % 5 = 4
15 % 5 = 0
With that you can identify if (and how large) such an uncomplete row would be. Knowing how many elements are in that last uncomplete row oviously allows you to calculate the number of remaining cells to fill the row.
($y+(($y%$x)?($x-($y%$x)):0))
...where $y is the number of items(e.g. 11) and $x is the number of items in a row(e.g. 5)

Categories