Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

problem with the do-until loop
Hi,



I want to generate 6 random numbers from 1 to 10.

The 6 numbers must not the same.

These numbers must match to the number of an array.

I like to know how many iterations to get the match.



I am having troubles with the do-until loop. Could someone point out the
problem for me?



#!/usr/bin/perl -w

# generate 6 random numbers (1~10) that match exactly to the @ticket

# count the number of loops



use strict;

srand(time||$$);



my @ticket = sort (2, 3, 5, 7, 8, 10);

my @all_number = ();

my @new_number = ();

my $count = 0;



# generate 10 numbers

for (my $i = 0; $i < 10; $i++) {

my $number = $i + 1;

push (@all_number, $number)

}



do {

#reset @new_number to the empty array each time through the loop.

@new_number = ();



#get 6 random numbers

for (my $i = 0; $i < 6; $i++) {

my $position = randomposition (@all_number);

# Pick a random number from @all_number

my $number = splice (@all_number, $position, 1);

push (@new_number, $number);

}

@new_number = sort (@new_number);

print "@new_number\n";

$count++;



} until (($new_number[0] eq "$ticket[0]") && ($new_number[1] eq
"$ticket[1]")

&& ($new_number[2] eq "$ticket[2]") && ($new_number[3] eq
"$ticket[3]")

&& ($new_number[4] eq "$ticket[4]") && ($new_number[5] eq
"$ticket[5]"));



print $count, "\n";



 ########################################
##################

sub randomposition {

my(@all_number) = @_;

# This expression returns a random number

return int(rand(scalar(@all_number)));

}



Report this thread to moderator Post Follow-up to this message
Old Post
Mei
06-06-05 08:56 PM


Re: problem with the do-until loop
It may all become much simpler once the syntax is cleaned up and
simplified.

For example, you can reduce

> my @all_number = ();
...
> # generate 10 numbers
>
> for (my $i = 0; $i < 10; $i++) {
>
>     my $number = $i + 1;
>
>     push (@all_number, $number)
>
> }

to

my @all_number = (1..10);

and reduce

>      #get 6 random numbers
>
>        for (my $i = 0; $i < 6; $i++) {
>
>         my $position = randomposition (@all_number);
>
>         # Pick a random number from @all_number
>
>         my $number = splice (@all_number, $position, 1);
>
>         push (@new_number, $number);
>
>       }

to

for (1..6) {
push @new_number, randomposition;
}

and

> sub randomposition {
>
>     my(@all_number) = @_;
>
>     # This expression returns a random number
>
>     return int(rand(scalar(@all_number)));
>
> }

to

sub randomposition {
# return random number between 1 and 10;
return int(rand 10)+1;
}

But I still don't quite understand what you are trying to accomplish.

Do you want to count how many tries it takes to get a copy of an array
by generating random numbers? What for? The count you would get will be
random anyway. There are statistical functions for this sort of thing.
Or maybe I completely misunderstood your purpose.


Report this thread to moderator Post Follow-up to this message
Old Post
mi.gg@alma.ch
06-08-05 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:47 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.