Solved

random numbers

Posted on 2001-06-12
5
140 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

21 Experts available now in Live!

Get 1:1 Help Now