in reply to random question

This is a subtle one!

There are quite a few issues here with your approach.

  1. Read the docs about the srand function. It says clearly "Do not call srand() (i.e., without an argument) more than once per process. The internal state of the random number generator should contain more entropy than can be provided by any seed, so calling srand() again actually loses randomness.". Therefore, it is of no use to reseed the random number generator unless you can guarantee that your seed is more random (has a higher entropy) than Perl's internal random number function.
  2. Did you try looking at the values of $randseed? It is critically important that they are highly random otherwise your call to srand is useless. What do you notice? Indeed, $randseed is always of the form SCALAR(0x281734c) and the hex-part will repeat after a while (after 11 turns on my computer, I think that has to do with reclaiming memory.) That gives you a very very low entropy. If you delete the backslash before the calls to the trim subroutine, it should improve the randomness.
  3. Only, actually it doesn't. Now you get the same random number after a few turns through the loop. The reason is that you try to be too clever. After the call to convertToAscii you get a loooong string of more than 200 characters of which you take the first 40 and you feed that to the srand function. But that is way above the capacity of srand. Look at the return value of srand. It will be 4294967295 (on my PC, it may perhaps be different between 32 and 64 bit Perl); ALWAYS! Because that is the maximum value srand accepts. So it is much better to take only a 9 digits string out of the result of convertToAscii, somewhere from the middle, which will give you a better seed value. Of course, you should really run some statistical tests to see whether all digits have an equal chance of being generated to make sure it is a good idea.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: random question
by Athanasius (Archbishop) on Sep 08, 2013 at 09:29 UTC

    Excellent points. I’ll add one more observation:

    The call to time() is presumably there to add entropy (randomness). But time returns the number of seconds since the epoch; and for a run of 101 values, the script completes in less than a second, so time() returns at most two different values! This adds almost nothing to the randomness of the result.

    I’m no mathematician, but I’d be surprised if the output of the algorithm being tried (when properly debugged) is any more “random” than Perl’s out-of-the-box equivalent:

    print int(rand(1e4)), "\n" for 0 .. 100;

    (Note that the first call to rand implicitly calls srand here.)

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The invocation of time() was just a stand in for something with more entropy. I really wasn't worried about that until i debuged it.

Re^2: random question
by perlaintdead (Scribe) on Sep 08, 2013 at 13:05 UTC

    I'm running at 64x and srand takes 19 chars

      Very well, but that is still less than half the length of the data you feed it.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics