一個(gè)班有50名學(xué)生要生成每人的考試答卷。
要求是:讓隨機(jī)的2人考95分以上、15人考80~90分、30人考60~80分、剩余的不及格;同時(shí)要求做到錯(cuò)題不能有完全一樣的試卷。也就是說(shuō),同學(xué)要隨機(jī)、分?jǐn)?shù)要隨機(jī)、錯(cuò)題也要隨機(jī)。
題型為:?jiǎn)芜x、不定項(xiàng)選擇與判斷題,單題分值在1、2、3、4、5分布。
請(qǐng)問(wèn)大佬們有沒(méi)好辦法,求指導(dǎo)。
-
暫時(shí)沒(méi)有搜索到此類似問(wèn)題。
<?php
// 題庫(kù)
$questions = [
['type' => 'single', 'content' => '單選題1', 'score' => 1],
['type' => 'single', 'content' => '單選題2', 'score' => 1],
['type' => 'multiple', 'content' => '不定項(xiàng)選擇題1', 'score' => 2],
['type' => 'multiple', 'content' => '不定項(xiàng)選擇題2', 'score' => 2],
['type' => 'judge', 'content' => '判斷題1', 'score' => 1],
['type' => 'judge', 'content' => '判斷題2', 'score' => 1],
];
// 生成試卷
function generatePaper($questions, $numStudents) {
$papers = [];
// 隨機(jī)生成2人考95分以上
$topStudents = array_rand(range(0, $numStudents-1), 2);
foreach ($topStudents as $student) {
$papers[$student] = generatePaperForStudent($questions, 95);
}
// 隨機(jī)生成15人考80~90分
$middleStudents = array_rand(array_diff(range(0, $numStudents-1), $topStudents), 15);
foreach ($middleStudents as $student) {
$papers[$student] = generatePaperForStudent($questions, rand(80, 90));
}
// 隨機(jī)生成剩余人數(shù)考60~80分
$remainingStudents = array_diff(range(0, $numStudents-1), $topStudents, $middleStudents);
foreach ($remainingStudents as $student) {
$papers[$student] = generatePaperForStudent($questions, rand(60, 80));
}
// 隨機(jī)生成錯(cuò)題
foreach ($papers as &$paper) {
$paper['wrongQuestions'] = generateWrongQuestions($questions);
}
return $papers;
}
// 為單個(gè)學(xué)生生成試卷
function generatePaperForStudent($questions, $score) {
$paper = [
'score' => $score,
'questions' => [],
];
$totalScore = 0;
while ($totalScore < $score) {
$question = $questions[array_rand($questions)];
if ($totalScore + $question['score'] <= $score) {
$paper['questions'][] = $question;
$totalScore += $question['score'];
}
}
return $paper;
}
// 生成錯(cuò)題
function generateWrongQuestions($questions) {
$wrongQuestions = [];
$numQuestions = count($questions);
$numWrong = rand(1, $numQuestions);
$wrongIndices = array_rand(range(0, $numQuestions-1), $numWrong);
foreach ($wrongIndices as $index) {
$wrongQuestions[] = $questions[$index];
}
return $wrongQuestions;
}
// 生成50名學(xué)生的試卷
$papers = generatePaper($questions, 50);
// 輸出試卷
foreach ($papers as $student => $paper) {
echo "學(xué)生" . ($student + 1) . "的試卷:\n";
echo "總分:" . $paper['score'] . "分\n";
echo "錯(cuò)題:\n";
foreach ($paper['wrongQuestions'] as $question) {
echo $question['content'] . "\n";
}
echo "\n";
}