亂數種子它的用法和mysql 類型資料庫完全不同,因為之前系統使用mysql 資料庫,目前改用 PostgreSQL 所以在題庫系統時會無法產生隨機取出題目問題
找了很久,找到了設定方法
mysql 是寫在 orderBy 內使用 rand(亂數種子)
亂數種子設定方法
How Setseed() Works in PostgreSQL
SELECT setseed(0.8), random(), random();
Result:
setseed | random | random ---------+--------------------+-------------------- | 0.7998745861323613 | 0.9776304992484732
Now, if I run the same statement again, I get exactly the same result:
setseed | random | random ---------+--------------------+-------------------- | 0.7998745861323613 | 0.9776304992484732
select setseed(0.8), * from datatable order by random() limit 10;
在 controaller 取得亂數種子
$rseed =$this->randseed();
public function randseed()
{
$max = 1;
$min = -1;
$range = $max - $min;
$num = $min + $range * (mt_rand() / mt_getrandmax());
$num = (float)round($num, 8);
return $num;
}
===============
select setseed(0.8), * from datatable order by random() limit 10;
這個要分為二行
select setseed(0.8)
select * from datatable order by random() limit 10;