@
@
@salomatin2019-10-31 23:53:10
PHP
@salomatin, 2019-10-31 23:53:10

How to make a generator of possible combinations in php?

The crux of the matter is this.
5dbb48d0c61ce674630326.jpeg
We have 15 lines, each line has 3 options. We can fix options in several from 15 lines.
After that, we enter the number of options (for example, 3) and the script should generate a result like
1-(1);2-(2);3-(1);4-(3);5-(1);6-(1 );7-(1);8-(1);9-(1);10-(1);11-(1);12-(1);13-(1);14-(1); 15-(1),
1-(1);2-(2);3-(1);4-(3);5-(2);6-(1);7-(1);8- (1);9-(1);10-(1);11-(1);12-(1);13-(1);14-(1);15-(1),
1-(1 );2-(2);3-(1);4-(3);5-(3);6-(1);7-(1);8-(1);9-(1); 10-(1);11-(1);12-(1);13-(1);14-(1);15-(1),
As you can see, the selected options remain unchanged, the rest change with each integration
Did does anyone like that?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
salomatin, 2019-11-01
_

In short, I made it myself, a little Hindu code, but everything works
// number of digits (For example 1-(1) 1-(2) 1-(3))
$n = 3;
// Number of fixed options (Can be calculated when sending via $_POST)
$k = 4;
$col_unic = pow($n, $k);
echo 'Factorial '.$col_unic.'
';
// You can put values ​​into array via $_POST (In this example, the first 4 are fixed, the rest will be filled in automatically)
$array = [
'1' => '1',
'2' => '3',
'3' => ' 1',
'4' => '2',
'5' => '',
'6' => '',
'7' => '',
'12' => '',
'13' => '',
'14' => '',
'15' => '',
];
$array_comb = array();
$array_all_comb = array();
// Number of rows
$count_combinations = 82;
// Check the number of possible options
$count_combinations = ($count_combinations >= $col_unic) ? $col_unic : $count_combinations;
function rand_chislo() {
global $array_comb;
global $array;
$array_comb[1] = ($array[1] == null) ? rand(1,3) : $array[1];
$array_comb[2] = ($array[2] == null) ? rand(1,3) : $array[2];
$array_comb[3] = ($array[3] == null) ? rand(1,3) : $array[3];
$array_comb[4] = ($array[4] == null) ? rand(1,3) : $array[4];
$array_comb[5] = ($array[5] == null) ? rand(1,3) : $array[5];
$array_comb[6] = ($array[6] == null) ? rand(1,3) : $array[6];
$array_comb[7] = ($array[7] == null) ? rand(1,3) : $array[7];
$array_comb[8] = ($array[8] == null) ? rand(1,3) : $array[8];
$array_comb[9] = ($array[9] == null) ? rand(1,3) : $array[9];
$array_comb[10] = ($array[10] == null) ? rand(1,3) : $array[10];
$array_comb[11] = ($array[11] == null) ? rand(1,3) : $array[11];
$array_comb[12] = ($array[12] == null) ? rand(1,3) : $array[12];
$array_comb[13] = ($array[13] == null) ? rand(1,3) : $array[13];
$array_comb[14] = ($array[14] == null) ? rand(1,3) : $array[14];
$array_comb[15] = ($array[15] == null) ? rand(1,3) : $array[15];
}
for ($i = 0; $i < $count_combinations; $i++) {
// Generate a sample
rand_chislo();
$array_result = '1-('.$array_comb[1].');2-('.$array_comb[2].');3-('.$array_comb[3].');4-(' .$array_comb[4].');5-('.$array_comb[5].');6-('.$array_comb[6].');7-('.$array_comb[7].' );8-('.$array_comb[8].');9-('.$array_comb[9].');10-('.$array_comb[10].');11-('.$ array_comb[11].');12-('.$array_comb[12].');13-('.$array_comb[13].');14-('.$array_comb[14].'); 15-('.$array_comb[15].'),';
$unique = 0;
while ($unique < 1) {
if (in_array($array_result,
$array_all_comb)) { //echo "There is such a selection";
// Generate a new sample
rand_number();
$array_result = '1-('.$array_comb[1].');2-('.$array_comb[2].');3-('.$array_comb[3].');4-(' .$array_comb[4].');5-('.$array_comb[5].');6-('.$array_comb[6].');7-('.$array_comb[7].' );8-('.$array_comb[8].');9-('.$array_comb[9].');10-('.$array_comb[10].');11-('.$ array_comb[11].');12-('.$array_comb[12].');13-('.$array_comb[13].');14-('.$array_comb[14].'); 15-('.$array_comb[15].'),';
}
else {
// echo 'New unique selection';
$unique = 1;
}
}
$array_all_comb += [$i => $array_result];
}
foreach ($array_all_comb as $key =>
$value) { //echo 'KEY - '.$key.' 50;'.$value.'
';
$value = str_replace("(2)", "(X)", $value);
$value = str_replace("(3)", "(2)", $value);
echo '50;'.$value.'
';
}
echo '
Number of samples '.count($array_all_comb);

A
Adamos, 2019-11-01
@Adamos

Number of choices = (number of choices per row) to the power of (number of rows).
Generate all possible options - you get better even on small numbers. Yes, no one needs it.
Generating an arbitrary variant is elementary.
Take an array of length in the number of lines, fill it with random numbers from 1 to the number of options.
Then, in the filled positions, replace this value with the number of the selected answer.
That's all combinatorics.

D
Daria Motorina, 2019-11-01
@glaphire

You can try
1. Transfer changeable elements from the common array to a new array
2. Combinatorially iterate through all the elements, generating an array of combinations
3. Substitute the array of combinations into the old array from step 1
I took the combination algorithm from here when I needed a similar task

P
profesor08, 2019-11-01
@profesor08

$variants = [1,5,7,20];

echo $variants[rand(0, count($variants) - 1)];

Apply to that rubbish from the screenshot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question