CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unique numbers

 



2bevil1
New User

Mar 4, 2008, 9:25 AM

Post #1 of 7 (2659 views)
Unique numbers Can't Post

Hello, Im new to perl.

Im looking for away to generate 6 unique two digit numbers between 1 and 47 with out assigning them to a hash to make them unique. This is for an assignment Im doing.



Thanks

Danny


eWish
User

Mar 4, 2008, 9:28 AM

Post #2 of 7 (2657 views)
Re: [2bevil1] Unique numbers [In reply to] Can't Post

What have you tried? Please do share the code with us.

--Kevin


KevinR
Veteran


Mar 4, 2008, 10:35 AM

Post #3 of 7 (2654 views)
Re: [2bevil1] Unique numbers [In reply to] Can't Post

The requirement to not use a hash makes no sense. Also, your number range makes no sense, 1 - 47. Do you mean 10 - 47?

You could use grep to make sure you have not repeated a number, but why you would want to makes no sense unless this is school work of some kind.
-------------------------------------------------


2bevil1
New User

Mar 6, 2008, 1:06 PM

Post #4 of 7 (2649 views)
Re: [KevinR] Unique numbers [In reply to] Can't Post

Sorry. I will clarify, the unique numbers need to be displayed as two digits 01-47.

The following code does it but assigns the random number to a hash to make them unique.

I do not want to assign the number to a hash to do this. Near the bottom I have some code that my instructor gave me but it is not working.



#!/usr/bin/perl



my @numbers = (1..47);

my $limit = 5;



# A hash used to make sure we have

# all unique numbers in the final list.

my %list = ();



# Here we generate the list of random

# unique numbers. The hash continues to

# build until there are $limit number

# of keys. Since hash keys must be unique

# we are left with all unique numbers

# without having to check anything except the

# total number of keys.

while (keys %list < $limit) {

$list{$numbers[rand @numbers]}=1;

}



# print sorted (ascending) list of numbers

print join(' ', sort {$a <=> $b} keys %list);





My instuctor gave me this code to use in my assignment, but I can not get it to work.

I need to generate random lotto numbers.



#!/usr/bin/perl -w

@x=uniq(5, 10, 20);
sub uniq($$$)
{
my ($n, $a, $b)=@_;
my $str="0"x($b-$a+1);
my $c=0;
my @x=();
while($c < $n)
{
$num = int(rand($a, $b));
if(substr($str, $num-$a,1)=="0")
{
$x[$c++]=$num;
}
}
return (@x)
}


Any help you can give me is much appriciated. Im very basic.



Thanks in advanced.


KevinR
Veteran


Mar 6, 2008, 3:54 PM

Post #5 of 7 (2642 views)
Re: [2bevil1] Unique numbers [In reply to] Can't Post

I wrote that bit of perl code you posted that uses the hash to generate the random numbers. It's a snippet I posted on dreamincode.com in a little while back.

You have too many arguments in the rand($a,$b) function, there should only be one arguments. If you want numbers 1 to 47 it should be:

rand(47)

that should get your script to run but it will not generate unique numbers.
-------------------------------------------------


(This post was edited by KevinR on Mar 6, 2008, 4:03 PM)


2bevil1
New User

Mar 6, 2008, 4:06 PM

Post #6 of 7 (2637 views)
Re: [KevinR] Unique numbers [In reply to] Can't Post

Thanks, this code is similar to what I need. It just can not be assigned to a hash first.

thanks


KevinR
Veteran


Mar 6, 2008, 7:33 PM

Post #7 of 7 (2630 views)
Re: [2bevil1] Unique numbers [In reply to] Can't Post


In Reply To
Thanks, this code is similar to what I need. It just can not be assigned to a hash first.

thanks


Well, that makes zero sense but you're the student (I guess) and he is the instructor. So you need to figure a way to get unique numbers without using a hash. Hint: grep
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives