[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Can someone help put bells and whisles?




Arthur Jacobs wrote:

> Hi,
> 
> I am new to programming, perl is my first language. I see the potential!
>  and I thought, hmmmm, "We have the technology! --I should be able to help
> people with Perl".
> 
> I teach at a Juvenile Detention Center where they have Macs (which is what
> I use) so again I thought (not so long), "hmm", that game hangman would get
> these kids interested in words, but we have to make it positive.  Instead
> of being hung for wrong letters getting released from jail for correct
> spelling!
> 
> I put a message up on a Perl Newsgroup for hangman thinking I could convert
> it to "Freeman" but the conversion was hard. However the man David Allan
> Black from Seton University in New Jersy came to my rescue. First with
> hangman and then we converted it to "Freeman".
> 
>  I put the program up on the web at
> http://www.sonic.net/~star/freeman3.html could you perl pro's check it out
> You may want to check the source (It will run if you take out  the '<p>'. I
> brought the game to the Juvenile hall today and I saw that it picks the
> same random word over and over. What code would I need so the word is taken
> out of the array if it was picked in that game already.
> 
> Arthur Jacobs
> star@sonic.net

For older versions of perl (older than 5.004 I think (?)) it was necessary
to call srand() (just like in the C programming language BTW), you will
probably want to pick "a good seed" for calling srand().  Some folks do 
chose algorithms based on the system time and/or multiple calls to srand() 
and/or rand() [but see below!].  It should not be necessary to get too 
fancy - just read the perlfunc doc:

-------------------------------------------------------------8<
=item srand EXPR

=item srand

Sets the random number seed for the C<rand> operator.  If EXPR is
omitted, uses a semi-random value based on the current time and process
ID, among other things.  In versions of Perl prior to 5.004 the default
seed was just the current time().  This isn't a particularly good seed,
so many old programs supply their own seed value (often C<time ^ $$> or
C<time ^ ($$ + ($$ << 15))>), but that isn't necessary any more.

In fact, it's usually not necessary to call srand() at all, because if
it is not called explicitly, it is called implicitly at the first use of
the C<rand> operator.  However, this was not the case in version of Perl
before 5.004, so if your script will run under older Perl versions, it
should call srand().

Note that you need something much more random than the default seed for
cryptographic purposes.  Checksumming the compressed output of one or more
rapidly changing operating system status programs is the usual method.  For
example:

    srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
                                      ^^^^^^^^^^^^^^^^^  UNIX ism

If you're particularly concerned with this, see the Math::TrulyRandom
module in CPAN.

Do I<not> call srand() multiple times in your program unless you know
exactly what you're doing and why you're doing it.  The point of the
function is to "seed" the rand() function so that rand() can produce
a different sequence each time you run your program.  Just do it once at the
top of your program, or you I<won't> get random numbers out of rand()!

Frequently called programs (like CGI scripts) that simply use

    time ^ $$

for a seed can fall prey to the mathematical property that

    a^b == (a+1)^(b+1)

one-third of the time.  So don't do that.

------------------------------------------------------------->8

Peter Prymmer
pvhp@forte.com


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch