logo       

Re: [SPOILER] Fred's Solution to Console Hangman Game: msg#00123

Subject: Re: [SPOILER] Fred's Solution to Console Hangman Game
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




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
linux.arklinux....    user-groups.lin...    kde.usability/2...    ietf.ipp/2002-0...    mail.spam.spamc...    os.netbsd.devel...    audio.cd-record...    text.unicode.de...    php.documentati...    games.fps.halfl...    window-managers...    suse.oracle.gen...    bug-tracking.gn...    video.dvdrip.us...    xfree86.cvs/200...    java.netbeans.m...    network.argus/2...    culture.sf.kill...    debian.ports.al...    freebsd.questio...    qplus.devel/200...    handhelds.palm....   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe