Complete.Org: Mailing Lists: Archives: discussion: April 2000:
[aclug-L] Re: Perl Random number!!
Home

[aclug-L] Re: Perl Random number!!

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] Re: Perl Random number!!
From: Jeff <schaller@xxxxxxxxxxxxx>
Date: Sun, 16 Apr 2000 08:27:44 -0500 (CDT)
Reply-to: discussion@xxxxxxxxx

On Sat, 15 Apr 2000, Mohammad Islam wrote:

> Hi,
> 
> I have a question about generating random number in perl.
> 
> What do i need to do to generate a different random number each time?

Remember which random number it gave you and loop until it gives
you a different one.

my $lastone=undef;
while ($you want more) {
        my $randnum = int(rand @tokens);
        while ($randnum == $lastone) {
                $randnum = int(rand @tokens);
        }
        $lastone = $randnum;
        # use $randnum
}


> Lets say I have an array called @tokens. It has five elements. I want to
> generate a random number and pick one of those elements randomly. Each
> time it has to be different than the previous one.
> 
> Say, i have the following code;
> 
> srand();
> 
> $myrandom = int(rand(@tokens));

By the way, you realize that $myrandom is really an index into the
@tokens array instead of the value itself -- $myrandom =
$tokens[int(rand @tokens)]; -- would give you the value from the
array.

-jeff
-- 
LAKE ERIE--Seeking to capitalize on the recent IPO rage on Wall Street, Lake
Erie-based blue-green algae Anabaena announced Tuesday that it will go public
next week with its first-ever stock offering.
http://www.theonion.com/onion3542/bluegreenalgae_ipo.html


-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]