Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RANDOM NUMBERs between 1 and 52

Status
Not open for further replies.

madaxe2

Technical User
Jan 23, 2005
43
US
I can generate random numbers between 1 and 52 but i want 9 differing numbers my code is below how can i ensure that all nine numbers are differant

Code:
my $lower=1; 
my $upper=52; 
my $random1 = int(rand( $upper-$lower+1 ) ) + $lower; 
my $random2 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random3 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random4 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random5 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random6 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random7 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random8 = int(rand( $upper-$lower+1 ) ) + $lower;
my $random9 = int(rand( $upper-$lower+1 ) ) + $lower;

print header;
print start_html("MCJEEVES.NET");

print $random1,"\n";
print $random2,"\n";
print $random3,"\n";
print $random4,"\n";
print $random5,"\n";
print $random6,"\n";
print $random7,"\n";
print $random8,"\n";
print $random9,"\n";


print end_html;


MADAXE
 
build an hash of already 'known' numbers, and if you have one already, go fish ...
Code:
if (exists($deck{'AceSpades'} (
  &gofish()
}

HTH
--Paul

cigless ...
 
one way might be:

Code:
use strict;
use warnings;
use CGI qw/:standard/;
my @shuffle = &get_random;
sub get_random {
   my @nums = (1..52);
   my %random = ();
   my $num = 0;
   until ($num == 9) {
      my $i = int($nums[rand(@nums)]);
      $random{$i}=1;
      $num = keys %random;
   }
   return(keys %random);
}
print header();
print start_html("MCJEEVES.NET");
print "$_\n" for @shuffle;
print end_html;

although to make this much more random I would shuffle the array: @nums = (1..52)
with each call to the sub routine. So that way you have an array of digits 1 thru 52 randomly ordered and then you randomly pull numbers from it until you have 9 uniques ones, which will be much more random than just using a fixed array of digits (1..52);
 
you have an array of digits 1 thru 52 randomly ordered and then you randomly pull numbers from it ... which will be much more random
I don't see what difference shuffling the pack will make - if you're selecting cards at random positions from it, the order of the cards in the deck isn't important.

If you're going to shuffle the deck, you might as well pick the first nine cards from it when you've finished (just like you'd do in real life). Shuffling is probably a more expensive process than picking 9 at random though.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
ChrisHunt,

Yes, picking the first nine "cards" after shuffling the array would be like a deal in real life, thats a good point to make.

My concern was that the rand function seems to slightly favor higher numbers than lower numbers. Running some tests might reveal if this is a valid concern or not.
 
Sorry Am i just beign dumb...blond ...english....male...etc....hahaah...but is'nt that the whole idea of """"RANDOM"""" i should not have to mix up me numbers??


MAdAxe
 
well, there are different levels of randomness. Some more random than others.

Use of Perl's rand() function is not advised for cryptographers. The rand() function uses a seed to generate its input, and unfortunately that seed is predictable approximately one third of the time. Check out perldoc -f srand and perldoc -f rand for more information on this. Instead, it's advised that for truly random input, one should use the Perl Module Math::TrulyRandom.

might not be necessary for your application but you may want to look into Math::TrulyRandom

I did a test of 1,000,000 iterations a few times and could not discern any patterns of unrandomness in the results. So rand might work OK for your application.
 
If you're worried about the less-than-total randomness of the rand() function, using it to shuffle, and then to pick the cards won't make the results any more random than doing just one or the other. The problem's still the same - if you can guess the seed you can predict the output. Personally I wouldn't worry about it.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
ok , things are now randomly clear but how do i make sure that the nine random numbers i choose all come out un equal to each other?


Madaxe
 
The code I posted will give you nine unique values, if thats what you are asking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top