Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Updating sub for random number generation

by Win (Novice)
on Dec 04, 2007 at 14:25 UTC ( [id://654799]=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Updating sub for random number generation
by Joost (Canon) on Dec 04, 2007 at 14:53 UTC
Re: Updating sub for random number generation
by tcf03 (Deacon) on Dec 04, 2007 at 15:10 UTC
    Like this?
    sub random_subset_X { my ($n, $k) = @_; # $n is no. of regions, $k no. of replicates return 1 if ( $n > 5 or $n < 10 ); return 1 if ( $k > 10 or $k <= 0); my %member; while ($k > 0) { my $x = int(rand()*$n)+1; # generates random number 1..$n. redo if $member{$x}; $member{$x} = 1; } continue { $k--; } sort keys %member; }

    Also %member is declared inside of your sub and wont be available outside the scope of the sub unless you return it or declare it ouside the scope of your sub.

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Updating sub for random number generation
by graff (Chancellor) on Dec 05, 2007 at 03:53 UTC
    I know you are trying, but you are still not making sense, and you have not yet supplied what toolic was asking for: assuming your "bit of code that activates this sub" is the update you made, you left a few things out (lots of variable declarations, sample input data values, desired results).

    If we start from your SQL query:

    SELECT DISTINCT Number_count FROM Region_lookup_for_eligible_regions
    clearly this will return a set of unique values (presumably all positive integers). Now the next step you want is... (I'm guessing) to generate a bunch of randomly selected subsets from this list of values? Something else? Let's suppose the list returned by that query is:
    2 4 5 8 9 12 15 17 21 22
    That's a list of 10 elements. (If your actual list is bigger or smaller, what sort of size should we expect?) Let's suppose further that these query results are stored in "@array", and you want to select 4 groups of randomly selected members from this list, such that each group contains 5 distinct elements. (I don't know if that's really what you want to do, because you have not yet provided, in either this thread or the other one on this topic, any clear example of what your desired output is.) Maybe something like this:
    #!/usr/bin/perl use strict; use List::Util qw/shuffle/; sub get_random_subsets { my ( $n_sets, $set_size, @superset ) = @_; my @sets; for my $set ( 1 .. $n_sets ) { push @sets, [ (shuffle( @superset ))[1..$set_size] ]; } return \@sets; } my @query_data = qw/2 4 5 8 9 12 15 17 21 22/; my $rand_sets = get_random_subsets( 4, 5, @query_data ); print "input superset: @query_data\n random subsets:\n"; for my $set ( @$rand_sets ) { print " @$set\n"; }
    You can run that and see what the output is. You can also play with the input values and the parameters for controlling the number and size of subsets. If there is actually something different that you want, you will need to provide a coherent example of what you really want.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://654799]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-17 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found