Solved

random words without repeating

Posted on 2001-06-15
7
138 Views
Last Modified: 2010-03-05
# I have to select three random words three times

@animals=("cat", "rat", "bat");
@unselectedkeys=(0..2);

 for(1..9)
  {
   if(@unselectedkeys==()){@unselectedkeys=(0..2)};

   $randomkey=int rand(@unselectedkeys);

   print $animals[$unselectedkeys[$randomkey]];

   @unselectedkeys=grep{$_!=$randomkey}@unselectedkeys;
 };
0
Comment
Question by:superquestions
7 Comments
 
LVL 1

Author Comment

by:superquestions
ID: 6196828
What I am trying to do is to select a random element from the array "animals" and keep track if I selected it so that I won't select it again.
0
 
LVL 1

Author Comment

by:superquestions
ID: 6196849
for(1..9){#instructions}

You can only modify the #instructions.
0
 
LVL 1

Author Comment

by:superquestions
ID: 6196884
# my debugging efforts

@animals=("cat", "rat", "bat");
@unselectedkeys=(0,1,2);

for(1..9)
 {
  # reset array when blank
  if(@unselectedkeys==()){@unselectedkeys=(0..2)};
 
  # select a random key
  $randomkey=int rand(@unselectedkeys);

  # remove the selected key
  @unselectedkeys=grep{$_!=$randomkey}@unselectedkeys;
};
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:superquestions
ID: 6196888
Why isn't the above script working properly?
0
 
LVL 6

Expert Comment

by:christopher sagayam
ID: 6197148
try changing

if(@unselectedkeys==()){@unselectedkeys=(0..2)};
 
 to

if($unselectedkeys[0] eq ""){@unselectedkeys=(0..2)};
 
 
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6197154

the problem is that you can get a rand that is not a value in the array...
because you taking the rand on the length an array(1,2) could yeild a rand of 0, but if your array was 1 and 2, then nothing is going to be removed, hence your code fails:


I will see if I can get the grep working, but this is a working solution:


# my debugging efforts

@animals=("cat", "rat", "bat");
@unselectedkeys=(0,1,2);

for(1..9)
{
 # reset array when blank
 if(@unselectedkeys==()){@unselectedkeys=(0..2)};
print @unselectedkeys;
 # select a random key
$randomkey=int rand(1+$#unselectedkeys);
 # remove the selected key
 for($i=0;$i<=$#unselectedkeys;$i++)
{
if($i!=$randomkey)
{
push @ok, $unselectedkeys[$i];
}
}
@unselectedkeys=@ok;
$#ok=-1;
};


Bob
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6197312
perldoc -q "How do I shuffle an array randomly?"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 130
perl script to count sepecial characters in a file 7 140
How to strip .csv from file name 9 78
Perl Untar File 1 27
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now