如何使用线性全等生成器应用随机问题?

时间:2016-04-27 16:37:00

标签: php android mysql json random

以前我使用rand()限制15的mysql顺序来生成随机问题,选择了15个问题并且结果是成功的。但是如果我想用线性全等发生器方法(LCG)随机化问题怎么办呢。

LCG的核心是以下公式:

X(i+1) = (a * X(i) + c) mod M

where 

M is the modulus. M > 0.
a is the multiplier, 0 <= a < M.
c is the increment, 0 <= c < M.
X(0) is the seed value, 0 <= X(0) < M.
i is the iterator. i < M 

我如何申请?以便使用LCG方法生成json格式的数据和随机结果,而不是使用rand()的顺序。

<?php
include("conn.php");
$arr = array();
$x   = mysql_query("select * from question_tbl order by rand() limit 15");
        while ($row = mysql_fetch_assoc($x)) {
            $temp = array(
                "id_question" => $row['id_question'],
                "question"=>$row['question'],
                "a"=>$row['a'],
                "b"=>$row['b'],
                "c" => $row['c'],
                "answer" => $row['answer'],
                "image" => "http://10.0.2.2:30/quiz/images/".$row['image'].""
            );
            array_push($arr, $temp);
        }   
    $data = json_encode($arr);
    $data = str_replace("\\", "", $data);
    echo "{\"question_list\":" . $data . "}";
?>

0 个答案:

没有答案