mercredi 27 avril 2016

Random questions with Linear Congruent Generator JSON Format

Previously I was using mysql order by rand () limit 15 to generate random questions with 15 questions selected and the results are successful. But what if I want to randomize the questions with Linear Congruent Generator method (LCG).

The heart of an LCG is the following formula:

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 

How do I apply? so as to generate data in json format with random results using LCG methods and not using the order by rand (). Thanks.

<?php
include("conn.php");
$arr = array();
$x   = mysql_query("select * from question_tbl");
        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://ift.tt/21foofx".$row['image'].""
            );
            array_push($arr, $temp);
        }   
    $data = json_encode($arr);
    $data = str_replace("\\", "", $data);
    echo "{\"question_list\":" . $data . "}";
?>




Aucun commentaire:

Enregistrer un commentaire