简体   繁体   English

使用php保持随机生成的数值

[英]Persisting the value randomly generated number using php

I am randomly generating numbers between 0 to 999 using shuffle function in php. 我使用php中的shuffle函数随机生成介于0到999之间的数字。 Is there a way to persist that order of those randomly generated numbers on each page reload 有没有办法在每个页面上重新加载那些随机生成的数字的顺序

<?php
session_start();

if (!isset($_SESSION['numbers'])) {
  $_SESSION['numbers'] = array(/*numbers here*/);
  shuffle($_SESSION['numbers']);
}

// use $_SESSION['numbers'] 

I think this is what you're after, but I might be wrong here. 这就是你所追求的,但我可能在这里错了。

You can select the seed value used for random number generation using the srand() function. 您可以使用srand()函数选择用于生成随机数的种子值。 This is the initial number which is used to generate all other random numbers from successive calls to rand() . 这是用于从连续调用rand()生成所有其他随机数的初始数字。 This means that if you pick a number (hell, even get one from rand() ) and store it, you can then use srand() to seed the generator and gain the same sequence of values back out from rand() . 这意味着如果你选择一个数字(地狱,甚至从rand()得到一个)并存储它,你就可以使用srand()为发生器播种并从rand()获取相同的值序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM