2

srand() and mt_srand() are disabled on my host server. I want to select a few random items from an array, but the should be the same on a particular day. Seeding with the date works on my computer, but not on the host server. (Damn these disable-happy security paranoid people!)

An external generator would be good, but I can't seem to find any. I'm not doing cryptography, the most basic of randomness would do.

EDIT: Done. I found this simple version in a related question and adapted the code for PHP. Perfect!

Community
  • 1
  • 1
Bacai
  • 81
  • 1
  • 6
  • `The question you're asking appears subjective and is likely to be closed.` This basically means the system saw your question as subjective, but since it's not, there's nothing to worry about. – esqew Jun 25 '11 at 16:45
  • I assume the "how can you..." start raised the suspicion of the script. Anyway, the readers of the site will decide if it's a real question or just an opinion poll, and in this case you can be sure that the question is legitimate. – vsz Jun 25 '11 at 16:49
  • you should post your EDIT as an answer and accept it as a correct. Thanks! – Alexey Jun 23 '17 at 16:59

2 Answers2

1

If it really is basic randomness you need, you could use something like MD5 to generate a hash from a predictable string (eg: the date in question).

Grab the first 8 characters, convert to decimal, divide by the maximum value possible (16 ^ 8 - 1 = 4294967295), and then you have a pseudo-random value between 0 and 1.

$seed = date('ymd', $theDate);
$hash = md5($seed); // eg: a9993e364706816aba3e25717850c26c

$num = hexdec(substr($hash, 0, 4)) / 4294967295; // 2845392438 / 4294967295 = 0.662494

$myRandomItem = $myArray[floor($num * count($myArray))];

If you need to choose more than one item, you could simply move on to the next 8 characters, or add a predictable, incrementing value to the seed:

for ($i = 0; $i < $numOfThingsINeed; ++$i) {
    $seed = date('ymd', $theDate) . $i;
    // etc
}
nickf
  • 537,072
  • 198
  • 649
  • 721
0

The site www.random.org has a good (and for some uses, free) random generator. For extra flavour it is even "true random", from atmospheric noise. I understand that you need the selection to be consistent through the whole day, but you can easily solve it by saving an array of numbers from the site and using them the whole day... if your system permits it, of course. This solution depends on a lot of factors (how big your list is), but if you download a new sequence of random numbers every day, you will not have the problem of bandwidth with an external site.

Edit: The site is quite reliable, but it has a downtime once in a while, so I recommend storing a few day's worth of random numbers.

Edit2, after some clarification:

If a simple approach is enough, why not write your own pseudo-random generator? If cryptography is not important, you can use any simple algorithm, like this one: http://en.wikipedia.org/wiki/Mersenne_twister, there is even a pseudocode implementation given in the article.

vsz
  • 4,811
  • 7
  • 41
  • 78
  • Well now, if I wanted to do that, I could just use the PHP mt_rand() to generate the sequence needed and save it in a file. The thing is, the dates are from 2008 to now so those are a lot of days. This is a possibility, but I want to do it so that I never have to deal with it again. – Bacai Jun 25 '11 at 16:51
  • So you need it to be based on a fixed seed so that you can call it retroactively for any past date, not just having a different seed every day? It was not clear enough in the original question. – vsz Jun 25 '11 at 16:58
  • That's what I did. I found [this simple version](http://stackoverflow.com/questions/506118/how-to-manually-generate-random-numbers/936184#936184) in a related question and adapted the code for PHP. Perfect! – Bacai Jun 25 '11 at 17:27