1

If I have a truely random number and I use mt_srand to seed mt_rand with the truly random number before I use mt_rand, will this mean the result of mt_rand is now also truly random rather than pseudorandom?

In otherwords, would the below code produce a truly random integer between the given minimum and maximum value

function RandomInteger($min, $max)
{
    $trueRandomNumber = GetTrueRandomNumber();

    mt_srand($trueRandomNumber);
    return mt_rand($min, $max);
}

Secondly, should the true random number used to seed mt_srand be of 32 integers?

Tesla
  • 793
  • 1
  • 10
  • 22
  • 1
    No. The result is always pseudo-random, see [the wikipedia article](http://en.wikipedia.org/wiki/Pseudorandom_number_generator) .. "A pseudorandom number generator (PRNG), also known as a deterministic random bit generator (DRBG), is an algorithm for generating a sequence of numbers that approximates the properties of random numbers. *The sequence is not truly random in that it is completely determined by a relatively small set of initial values, called the PRNG's state, which **includes** a truly random seed.*" –  Aug 04 '12 at 17:06
  • A better question/answer might analyze the distribution of Truly Random Seeds in relationship to the (First) Generated Random Numbers. I wouldn't be surprised if there were doctoral thesis on it .. –  Aug 04 '12 at 17:10

2 Answers2

1

No. Mersenne twisters are always pseudorandom. The seed only determines where in the sequence it starts.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • But if the seed is truly random, that means the sequence it follows each time is random, doesnt that effectively make the result of mt_rand truly random? – Tesla Aug 04 '12 at 17:09
  • No. It only means that you don't know where in the sequence it will start. It still follows the same sequence each time regardless. – Ignacio Vazquez-Abrams Aug 04 '12 at 17:10
  • @Tesla It can't, by definition :-) It *could* mean that the resulting (first) number is "truly randomly distributed" .. but I've not done or read about this level of statistical analysis so .. no guarantees. –  Aug 04 '12 at 17:11
  • (Although considering that MT already has very good distribution properties ..) –  Aug 04 '12 at 17:18
  • so I must be misunderstanding PNGs. If my PNG has a sequence 7 2 5 4 0 8 6 1 3 9 My understanding is the problems with PNGs are that its possible to predict what the next number is, and eventually the PNG repeats the sequence. But if I am entering the sequence at a random point each time, doesnt that mean the number chosen is effectively random because I am only choosing one number each time before seeding again and choosing a random point to enter the sequence again, thereby eliminating both the above problems with a PNG in that you can't predict the next number and the sequence wont repeat? – Tesla Aug 04 '12 at 17:29
  • 3
    If you have enough truly random seeds that you can seed the MT each time... then why aren't you just using the seeds as your random numbers? – Ignacio Vazquez-Abrams Aug 04 '12 at 17:32
  • @Ignacio, because it would be the best solution to my original problem which you can see here http://stackoverflow.com/questions/10896997/how-to-get-integer-between-two-values-from-stream-of-bits – Tesla Aug 04 '12 at 17:36
  • Using it as a seed doesn't really get you any further than Jack's first two methods though. – Ignacio Vazquez-Abrams Aug 04 '12 at 17:43
1

Looking at your code, my guess is that you are getting some value from GetTrueRandomNumber() (code is missing) but then you want that number to be in a specific range of values. So you are taking that output and inputting it into mt_rand() because it has a method of generating a number in a specific range.

While not a direct answer to your question, a better solution is to first figure out the range of values you want (i.e. as if the input $min was 0 and $max was $max - $min). Then, figure out the maximum number of bits that are required to obtain a value in that range. Then, extract that number of bits from the output of GetTrueRandomNumber(). If the value is within the range, return the number + the original $min value. If the value isn't within the range, get more bits of data. The key is to throw away bits until you get some in the desired range.

If need example source code for this, try:

http://barebonescms.com/documentation/csprng/

You should be able to put something similar together. I'd be wary of using mt_rand() for anything like this but it might be okay in this very specific instance. It depends on what you plan on using it for. It also depends highly on how well distributed the first number from Mersenne Twister actually is. I don't think anyone's done any work on that. MT is intended to be seeded one time - who knows what the distribution pattern is for repeatedly seeding it. Also, if other code uses mt_rand(), you risk exposure of your function's state based on later values that might get generated by later mt_rand() calls.

CubicleSoft
  • 2,274
  • 24
  • 20
  • Thanks for your answer, yes you have understood what I am trying to achieve, and your solution sounds similar to what I have considered already, can you quickly check my answer here http://stackoverflow.com/a/11114942/1437296 and tell me if thats in line with what you are saying (sounds very similar). That is what I was going to go with but I just wanted to check if it can be done by using mt_rand instead but from what I gather from your answer and other people's answers mt_rand is not the way to go. – Tesla Aug 06 '12 at 07:38
  • 1
    Your answer looks about right, but I'd use a while loop instead of recursion. – CubicleSoft Aug 06 '12 at 16:11