Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Calaban's scratchpad

by Calaban (Beadle)
on Jun 01, 2004 at 19:43 UTC ( [id://358467]=scratchpad: print w/replies, xml ) Need Help??

Had to come up with sets of random numbers where the numbers chosen were unique within the set. After I wrote this I thought - man, do you know what a hash is? I am in the process of redoing this project with a hash instead of an array. Any other help would be much appreciated.
#!/usr/bin/perl -w use strict; use CGI qw(:standard); print header; print start_html('The RUNG Answer'), h1('Random *Unique Number Generator'), "Random numbers will be unique within each set. ", "Each row of output represents one set. ", start_form, "How many numbers per set? ",textfield(-name=>'cnt', -size=>3, -maxlength=>3), p, "How many sets? ",textfield(-name=>'sets', -size=>3, -maxlength=>3), p, "Lowest number ? ",textfield(-name=>'low_end', -size=>5, -maxlength=>5), p, "Highest number ? ",textfield(-name=>'high_end', -size=>5, -maxlength=>5), p, submit(-name=>'Submit')," ",defaults(-name=>'Reset'), end_form, hr; if (param) { my $start; my @random_numbers; my $low_end = int(param('low_end')); my $high_end = int(param('high_end')); my $cnt = int(param('cnt')); my $sets = int(param('sets')); my $set; my $done; if ($cnt > 100) { CntErrorMessage(); exit 1; } if ($sets > 100) { SetsErrorMessage(); exit 1; } if ($low_end >= $high_end) { LowHighErrorMessage(); exit 1; } if ($cnt > ($high_end - $low_end + 1)) { OverloadErrorMessage(); exit 1; } for (1..$sets){ $start = 1; $done = 0; do{ GetUniqueRandomNumbers(); } until $done; @random_numbers = sort by_number @random_numbers; $set = join(" : ", @random_numbers); print "<P>$set</P>"; @random_numbers = (); } print end_html; #subroutines sub GetUniqueRandomNumbers{ my $unique = 0; my $current_number = GetRandomNumber(); if ($start) { push(@random_numbers,$current_number); $start = 0; } else { foreach (@random_numbers){ if ($_ != $current_number){ $unique++; } } if ($unique == scalar(@random_numbers)){ push(@random_numbers,$current_number); } } if (scalar(@random_numbers) == $cnt){ $done = 1; } } sub GetRandomNumber{ my $random_number = rand($high_end - $low_end) + $low_end; return sprintf("%.0f", $random_number); } sub by_number{ if ($a < $b){ return -1; } elsif ($a == $b) { return 0; } elsif ($a > $b) { return 1; } } sub CntErrorMessage{ print "There is a limit of 100 random numbers per set."; print end_html; } sub SetsErrorMessage{ print "There is a limit of 100 sets of random numbers."; print end_html; } sub OverloadErrorMessage{ print "I cannot give you unique numbers in this case, decrease the +amount of numbers you want or increase your range."; print end_html; } sub LowHighErrorMessage{ print "Check your range. Your low end number should be lower than +the high end number."; print end_html; } }
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found