Fred P. writes:
> use vars qw/
...
> $HAS_MATH_RANDOM
...
> /;
So far as I can see that variable never gets assigned to. I suspect
that it's supposed to be in here:
> #
> # Math::Random
> # http://search.cpan.org/~grommel/Math-Random-0.67/Random.pm
> #
> eval
> {
> require Math::Random;
> };
>
> if ( $@ )
> {
> print "Using normal random function\n" if ( $DEBUG );
> }
> else
> {
Setting it to true at this point?
> print "Using Math::Random\n" if ( $DEBUG );
> }
> }
On a different matter:
> sub initRandom()
> {
> # Programming Perl, page 223
> # Mangle time with PID for more randomness
> # especially for CGI scripts
> my $seed = (time()) ^ ( $$ + ($$<<15));
>
> # Doesn't work that great
> srand( $seed ) if ( $$ > 0 );
> }
Which edition of 'The Camel Book' is that from? I was under the
impression that the best way to get random numbers is now to leave srand
alone and let Perl get on with it -- or is the above still current
advice?
> sub pickWord($)
> {
> my $file_path = $_[0];
>
> # Programming Perl, page 85
I don't have a copy of that book to hand, but I'm intrigued as to what
the code on page 85 is for -- presumably not actually picking a random
word from a file? I'm not convinced by this algorithm:
> my $file_size = ( -s $file_path );
> my $range = getRange( $file_size );
> my $word;
>
> open( FH, "< $file_path\0" );
> seek( FH, $range, 0 );
That seeks to a random byte in the file. That might be part-way through
a word; in fact it's quite likely to be. And since each byte has a
random chance of being the chosen one, and some words consist of more
bytes than others, it's more likely to be in the middle of a longer word
than a shorter one.
> my $first_word = <FH> || "";
> my $second_word = <FH> || "";
>
> chomp $first_word if ( $first_word );
> chomp $second_word if ( $second_word );
>
> if ( $range == 0 )
[ edge case for being at the very start of the file snipped ]
> elsif (!$second_word || $second_word !~ /\w+/ )
[ edge case for being at the end of the file snipped ]
> else
> {
> $word = $second_word;
> }
Because we want to pick a whole word, and $first_word is quite likely
only part of a word, the second word read in is usually chosen.
That gives a rather interesting distribution: words do not have equal
chance of being picked, and the thing that determines a particular
word's chance is the length of the word that precedes it in the
dictionary file!
Smylers
|