• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

random numbers

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
superquestions
Asked:
superquestions
  • 4
1 Solution
 
bebonhamCommented:

@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
 
ozoCommented:
#You probably meant
print $keys[rand(@keys)]
0
 
bebonhamCommented:
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
 
bebonhamCommented:
my previous posts have answered the question.
0
 
bebonhamCommented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now