2 回答

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
另一種可能的實(shí)現(xiàn)
<?php
$classAPossibleOccuences = [0,1];
$classARandomChosenOccurence = $classAPossibleOccuences[array_rand($classAPossibleOccuences)];
$classBPossibleOccuences = [3,6];
$classBRandomChosenOccurence = $classBPossibleOccuences[array_rand($classBPossibleOccuences)];
$classEPossibleOccuences = [0,2];
$classERandomChosenOccurence = $classEPossibleOccuences[array_rand($classEPossibleOccuences)];
$classFPossibleOccuences = [0,1];
$classFRandomChosenOccurence = $classFPossibleOccuences[array_rand($classFPossibleOccuences)];
$generatedRandomClassesArr = [];
if($classARandomChosenOccurence) {
$generatedRandomClassesArr[] = 'class-A';
}
if($classFRandomChosenOccurence) {
$generatedRandomClassesArr[] = 'class-F';
}
for($i = 0; $i < $classBRandomChosenOccurence; $i++) {
$generatedRandomClassesArr[] = 'class-B';
}
if($classERandomChosenOccurence) {
for($i = 0; $i < $classERandomChosenOccurence; $i++) {
$generatedRandomClassesArr[] = 'class-E';
}
}
while(sizeof($generatedRandomClassesArr) < 12) {
$randomCOrD = ['C','D'];
$randomClass = $randomCOrD[array_rand($randomCOrD)];
$generatedRandomClassesArr[] = 'class-' . $randomClass;
}
//randomize array elements
shuffle($generatedRandomClassesArr);
highlight_string("<?php\n\$generatedRandomClassesArr =\n" . var_export($generatedRandomClassesArr, true) . ";\n?>");
echo '<br><br>';
highlight_string("<?php\n\$countValues =\n" . var_export(array_count_values($generatedRandomClassesArr), true) . ";\n?>");
?>
檢查工作演示

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
我在這里實(shí)現(xiàn)了一個(gè)工作演示,它必須滿足你的所有規(guī)則
<?php
$randomClassesArr = [];
while (sizeof($randomClassesArr) <= 12) {
$letter = chr(rand(65,70));
$classLetter = 'class-' . $letter;
if($letter === 'A' || $letter === 'F') {
if(!in_array($classLetter, $randomClassesArr)) {
$randomClassesArr[] = $classLetter;
}
} elseif($letter === 'B') {
$occurences = array_count_values($randomClassesArr);
$classBOccurence = $occurences[$classLetter] ?? 0;
if($classBOccurence !== 3) {
$randomClassesArr[] = $classLetter;
}
} elseif($letter === 'E') {
$occurences = array_count_values($randomClassesArr);
$classEOccurence = $occurences[$classLetter] ?? 0;
if($classEOccurence !== 2) {
$randomClassesArr[] = $classLetter;
}
} else {
$randomClassesArr[] = $classLetter;
}
}
highlight_string("<?php\n\$randomClassesArr =\n" . var_export($randomClassesArr, true) . ";\n?>");
echo '<br><br>';
highlight_string("<?php\n\$countValues =\n" . var_export(array_count_values($randomClassesArr), true) . ";\n?>");
?>
在此處查看工作演示,并隨時(shí)在其之上進(jìn)行編輯。
- 2 回答
- 0 關(guān)注
- 95 瀏覽
添加回答
舉報(bào)