Set::CrossProduct -> random

D

David

The documentation for this module says that the random() method
returns a random tuple from the cross product ... I'm stuck, using v.
1.4 calling random always returns the same tuple :

my $i = Set::CrossProduct->new(\@listoflists);
while (1) {
my @list = $i->random();
do something with @list
}

The relevant code from the module is :

sub random
{
my $self = shift;

my @array = map {${ $self->{arrays}[$_] }[
rand($self->{counters}[$_]] }
0 .. $#{ $self->{arrays} };

if( wantarray ) { return @array }
else { return \@array }
}

and initiating rand with something like :

srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);

in my code has no effect. Any help gratefully received - a search of
clpm yielded nothing on this module. Thanks in advance.
David
 
B

Bob Walton

David said:
The documentation for this module says that the random() method
returns a random tuple from the cross product ... I'm stuck, using v.
1.4 calling random always returns the same tuple :

my $i = Set::CrossProduct->new(\@listoflists);
while (1) {
my @list = $i->random();
do something with @list
}

The relevant code from the module is :

sub random
{
my $self = shift;

my @array = map {${ $self->{arrays}[$_] }[
rand($self->{counters}[$_]] }
0 .. $#{ $self->{arrays} };

if( wantarray ) { return @array }
else { return \@array }
}

and initiating rand with something like :

srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);

in my code has no effect. Any help gratefully received - a search of
clpm yielded nothing on this module. Thanks in advance.
David

Looks like there are a couple of bugs in the Set::CrossProduct module.
Try replacing sub random in that module with:

sub random
{
my $self = shift;

my @array = map { ${ $self->{arrays}[$_] }
[ rand($self->{lengths}[$_]+1) ] }
0 .. $#{ $self->{arrays} };

if( wantarray ) { return @array }
else { return \@array }
}

Warning: This revision is not very thoroughly tested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top