Home > Backend Development > PHP Tutorial > How to control the order of appearance of elements through probability after the PHP array is shuffled?

How to control the order of appearance of elements through probability after the PHP array is shuffled?

WBOY
Release: 2024-05-02 18:51:02
Original
1016 people have browsed it

Methods in PHP to control the order of appearance of array elements based on probability: To disrupt the order of the array: use the shuffle() function. Assign probabilities: Use array_map() to assign a probability (0-1) to each element. Sort weighted array: Sort the array in descending order of probability (elements with higher probability first). Extract elements: Extract elements from the sorted array in sequence, that is, scramble the array in a probability-controlled order.

How to control the order of appearance of elements through probability after the PHP array is shuffled?

Using probability control in PHP to scramble the order of appearance of array elements

Disrupting the order of elements in an array is a common problem in many development scenarios Common requirements, such as random draws, game mechanisms, etc. If you need to control the order of appearance of elements according to probability after shuffling the array, PHP provides appropriate functions and methods.

Preparation

First, prepare an array containing the elements to be scrambled. For example:

$array = [1, 2, 3, 4, 5];
Copy after login

Use the shuffle() function to shuffle the array

shuffle() The function can shuffle the array The order of the elements.

shuffle($array);
Copy after login

Use probability to control the order in which elements appear

Use the array_map() method to traverse the array elements and assign a probability to each element. The probability value ranges from 0 to 1, where 0 means that the element will never appear and 1 means that the element will always appear.

$probabilities = [0.2, 0.3, 0.4, 0.5, 0.6];
$weightedArray = array_map(function ($element, $probability) {
    return [$element, $probability];
}, $array, $probabilities);
Copy after login

Sort weighted array

Sort the weighted array, the element with higher probability will be at the beginning of the array.

usort($weightedArray, function ($a, $b) {
    return $b[1] <=> $a[1];
});
Copy after login

Extract elements

Now, extract elements from the sorted weighted array in sequence to obtain a scrambled array in a probability-controlled order.

$reorderedArray = [];
foreach ($weightedArray as $element) {
    $reorderedArray[] = $element[0];
}
Copy after login

Practical Case

Suppose there is a game that requires randomly selecting 3 items from an array containing 5 items. The probability of each item appearing is:

  • Item 1: 20%
  • Item 2: 30%
  • Item 3: 40%
  • Item 4: 50%
  • Item 5: 60%

You can follow the above steps to implement the following code:

$items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
$probabilities = [0.2, 0.3, 0.4, 0.5, 0.6];

shuffle($items);
$weightedItems = array_map(function ($item, $probability) {
    return [$item, $probability];
}, $items, $probabilities);

usort($weightedItems, function ($a, $b) {
    return $b[1] <=> $a[1];
});

$drawnItems = [];
for ($i = 0; $i < 3; $i++) {
    $drawnItems[] = $weightedItems[$i][0];
}

print_r($drawnItems);
Copy after login

Run the above code and control the output according to probability 3 items drawn sequentially.

The above is the detailed content of How to control the order of appearance of elements through probability after the PHP array is shuffled?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template