I don’t speak English well, I apologize in advance
Part 1. What and how it works
First, I’ll tell you what the task involves, you need to create a certain algorithm, the input of which is an array with items that have a price ('cost' => 123
), and also the input is the cost of one obtaining a skin $get_cost = 123;
, example data:
$get_cost = 10;
$items = [
['cost' => 5],
['cost' => 10],
['cost' => 20]
];
Also, thanks to the selection method and 2 hours of spent time, I have already calculated the chances for some input data, for example, for the data that I showed above, all even chances are equal:
+———–+—–——–+——–—+
| 5 | 10 | 20 |
+——––+——–––+—–——+
| 0 | 100 | 0 |
| 10 | 85 | 5 |
| 20 | 70 | 10 |
| 30 | 55 | 15 |
| 40 | 40 | 20 |
| 50 | 25 | 25 |
| 60 | 10 | 30 |
+——––+——–––+—–——+
*I erased some data so that it would not bother you, the data I erased was needed for selection and so on, just in case
The table above shows possible even percentages for these numbers, for example, take line 4, 5 – 30%, 10 – 55%, 20 – 15%.
Now I’ll tell you how it works, for example, let’s select 10 million items based on the given chances. times, for each sample (attempt) we spend the balance, we write how much we spent in the conditional $sum_spending = 0;
and we get the following code:
var get_cost = 10;
var sum = 0;
for (var i = 0; i < 10000000; i++) {
var rand = Math.floor(Math.random() * 100) + 1 // generate a number from 1 to 100
if (rand <= 40) sum += 5; // 40% chance
else if (rand <= 80) sum += 10; // 40% chance
else if (rand <= 100) sum += 20; // 20% chance
}
document.getElementById('cost_get').innerHTML = i * get_cost;
document.getElementById('sum_get').innerHTML = sum;
<!-- language: lang-html -->
<div id="cost_get"></div>
<div id="sum_get"></div>
I rewrote the code in js so that we could test it, this code works like this, the first has a chance of, for example, 40%, so in the first condition we set something like:
if (range less than or equal to 40)
,
to the next condition we add the previous chance of 40% + 40% and it turns out:
if (range is less than or equal to 80 and greater than 40)
and so on, this is the most obvious example of choosing a random element based on the chances, only it does not select, but immediately sums it up, this is done so that all these million-dollar operations are performed in a couple of seconds,
Also, if you need a function that will select from an array, the array will already look like this:
$items = [
['cost' => 5, 'chance' => 40],
['cost' => 10, 'chance' => 40],
['cost' => 20, 'chance' => 20]
];
Here is the code that selects based on the specified chances (the total chances should be 100)
$items = [
['cost' => 5, 'chance' => 40],
['cost' => 10, 'chance' => 40],
['cost' => 20, 'chance' => 20]
];
foreach ($items as $key => &$item) {
if (isset($items[$key - 1])) $item['chance'] += $items[$key - 1]['chance'];
}
function getRandItem($items) {
$rand = rand(1, 100);
foreach ($items as $item) {
if ($rand <= $item['chance']) return $item;
}
}
print_r(getRandItem($items));
Part 2. Theory and description
From the very beginning I didn’t understand how it works, I read an article about probability theory,
https://ru.wikipedia.org/wiki/%D0%A2%D0%B5%D0%BE%D1%80%D0%B8%D1%8F_%D0%B2%D0%B5%D1%80%D0 %BE%D1%8F%D1%82%D0%BD%D0%BE%D1%81%D1%82%D0%B5%D0%B9
After searching Wikipedia, I came to the conclusion that we need to use Laplace distribution
https://ru.wikipedia.org/wiki/%D0%A0%D0%B0%D1%81%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D0 %B5%D0%BD%D0%B8%D0%B5_%D0%9B%D0%B0%D0%BF%D0%BB%D0%B0%D1%81%D0%B0
But that’s just my opinion.
Let’s define the principle of chances, having written down all these chances, I found a certain pattern, for example, take the following table:
+—————–———+——————————+———–——–+
| 5 | 20 | SUM |
+—————–———+——————————+———–——–+
| 66.6666 | 33.3333 | ~100% |
+—————–———+——————————+———–——–+
If there are 2 elements, then there is only 1 chance option, if there are 3 elements, then the number of options increases significantly up to infinity:
+—————+———––+——––+––––––+
| 2.5 | 10 | 40 | SUM |
+—————+———––+——––+––––––+
| 0 | 100 | 0 | 100% |
| 20 | 75 | 5 | 100% |
| 40 | 50 | 10 | 100% |
| 60 | 25 | 15 | 100% |
| 80 | 0 | 20 | 100% |
+—————+———––+——––+––––––+
Also, there is always 1 or more chances for input values, Exception if all input values are less than the amount received $get_cost or more (In all the above examples $get_cost is equal to = 10),
If there is an input parameter equal to $get_cost, there will always be an option that it is equal to 100%.
If there are 3 or more input parameters and there is a parameter equal to $get_cost, there will always be the option that the chance of a parameter equal to $get_cost will be 0%. This can be seen in the example higher.
Part 3. How to calculate odds?
Chances always increase and decrease linearly and with a certain pattern, here are a couple more examples:
+———–+––———+——–––+———–––+
| 1 | 10 | 100 | SUM |
+———–+––———+——–––+———–––+
| 0 | 100 | 0 | 100% |
| 50 | 45 | 5 | 100% |
+———–+––———+——–––+———–––+
+———–+———––+–———+——–––+––————+
| 5 | 10 | 20 | 40 | SUM |
+———–+———––+–———+——–––+––————+
| 0 | 100 | 0 | 0 | 100% |
| 20 | 70 | 10 | 0 | 100% |
| 40 | 50 | 5 | 5 | 100% |
| 50 | 25 | 15 | 5 | 95% | // May not be used
| 60 | 30 | 0 | 10 | 100% |
| 80 | 0 | 10 | 10 | 100% |
+———–+———––+–———+——–––+––————+
I wasn’t smart enough to find some specific formula that would, based on the input parameters, give out similar chances, if someone is smarter and has better logic, please help me, I will be extremely grateful, I can give a monetary reward (I hope this not prohibited).
You can also write in any language, if you interpret something for yourself.
I would also like to note that a lot of the code was simplified for better understanding, and I also tried to explain the task in understandable language.
I apologize in advance for any mistakes in words, if any are observed, I’ve been writing this question for about 4 hours now 🙂
Thank you for your attention!
David Meyster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.