develooper Front page | perl.beginners | Postings from March 2002

random word from array

Thread Next
March 27, 2002 16:02
random word from array
Message ID:
I decided to write a little script to help choose who will make the tea on
our gaming night [there is always an argument!]

I thought I was doing quite well but it seems I am picked on by the
[non]random script I wrote! It seems to default to the first word in the

I used rand @array;

I think perhaps that I am thinking about it too simply and perhaps I need to
involve more math - but I am not good at maths [though I am willing and a
quick] ! so can anyone point me in the right direction?


@tea = "Meba", "Shaun", "Mark", "Jason", "Rick", "Dan";

#below is the problem area I think - perhaps rand is not appropriate here?
#or I haven't phrased it right?
#I have looked through masses of documentation and faqs but can't find a
#Am I blind or just dim?

$get = rand @tea;
$who = $tea[$get];

$affirm = "The lucky tea maker is ";

$result = $affirm.$who;

$fail = "Sorry I wasn't listening. Try again!\n";

print "Enter y to see who will make the tea: ";
$input = <STDIN>;
chomp $input;

if ($input eq "y") {
      $a= true;

if ($a) {print $result
 print "$fail" };

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About