Solved

random numbers

Posted on 2001-06-12
5
146 Views
Last Modified: 2010-03-05
I need to generate a random number from 1 to 15 with one condition: it can not be any of the numbers listed on @alreadyusedbanners!!!
0
Comment
Question by:superquestions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6183801

@alreadyusedbanners=(4,3,7,1);
for(1..15){$numbs{$_}=$_};
foreach(@alreadyusedbanners){delete $numbs{$_}};
@keys=keys %numbs;
$id=rand($#keys);
print $numbs{$keys[$id]} . "\n";
















0
 
LVL 84

Expert Comment

by:ozo
ID: 6184449
#You probably meant
print $keys[rand(@keys)]
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6185787
yeah, that is better, and if we aren't going to use the hash, in the end, lets just avoid it because it is slower and takes more memory than an array I think.

@alreadyusedbanners=(4,3,7,1);
NUMBER: for(1..15){
foreach $used(@alreadyusedbanners){next NUMBER if $_==$used};
push(@keys, $_)};
print $keys[rand(@keys)];


Bob
0
 
LVL 8

Accepted Solution

by:
bebonham earned 75 total points
ID: 6200454
my previous posts have answered the question.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6200906
thanks
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

705 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