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

In reply to Re: random question by CountZero
in thread random question by perlaintdead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.