Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Weighting rand()

by hacker (Priest)
on May 25, 2004 at 07:47 UTC ( [id://356129]=perlquestion: print w/replies, xml ) Need Help??

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

I have an array of mirrors that I am using to help us distribute some of our Free Software releases. Currently, when I output the links on our download page, I am using something like this:
my @mirrors = ( 'http://foo.mirror.org', 'http://www.users-domain.com/proj/foo', 'http://osdn.dl.sf.net/sourceforge/foo', 'http://unc.dl.sf.net/sourceforge/foo', 'http://umn.dl.sf.net/sourceforge/foo', 'http://heanet.dl.sf.net/sourceforge/foo', 'http://aleron.dl.sf.net/sourceforge/foo', ); print $cgi->a({-href => "$mirrors[rand @mirrors]/$file-$stable.rpm", -title =>"$stable RPM"}, 'rpm');

So far, this works for randomizing the links we have on our download page. What I'd like to do though, as some mirrors have a throttling limit and a monthly bandwidth quota enforced by their providers, is find a way to "weight" the randomization, so that some of the elements in @mirrors are chosen "more often" than the others. In the example pseudocode above, the Sourceforge mirrors would be weighted higher than the users' home DSL connections who are helping out by being "part-time" mirror sites.

What is the best way to go about handling this?

Replies are listed 'Best First'.
Re: Weighting rand()
by EdwardG (Vicar) on May 25, 2004 at 08:02 UTC

    You could change the distribution of @mirrors like this

    my @mirrors; push @mirrors, 'http://foo.mirror.org' for 1..3; push @mirrors, 'http://www.users-domain.com/proj/foo' for 1..2; push @mirrors, 'http://osdn.dl.sf.net/sourceforge/foo' for 1..3; push @mirrors, 'http://unc.dl.sf.net/sourceforge/foo' for 1..4; push @mirrors, 'http://umn.dl.sf.net/sourceforge/foo' for 1..4; push @mirrors, 'http://heanet.dl.sf.net/sourceforge/foo' for 1..5; push @mirrors, 'http://aleron.dl.sf.net/sourceforge/foo' for 1..5; print $cgi->a({-href => "$mirrors[int(rand(@mirrors))]/$file-$stable.rpm", -title =>"$stable RPM"}, 'rpm');

     

Re: Weighting rand()
by BrowserUk (Patriarch) on May 25, 2004 at 08:13 UTC

    Rather than duplicating the array entries, I'd duplicate the indexes.

    my $pick = ( (0)x3, (1)x4, (2)x4, (3)x5, (4)x6, (5)x6, (6)x6, (7)x6 )[ rand 40 ]; print $cgi->a({-href => "$mirrors[ $pick ]/$file-$stable.rpm", -title =>"$stable RPM"}, 'rpm');

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      This uses less memory but introduces a more difficult synchronisation task when mirrors are changed.

       

        Actually, it probably doesn't save any memory either.

        The size of a scalar has a minimum overhead. Anything below about 20 characters (guess) in length requires the same amount of memory, so the fact that the integers appear smaller than the string will have negligable savings.

        Worse, doing it my way requires a (short lived) second array (list) to be built, which will consume almost as much memory as the original array, thereby doubling the consumption breifly.

        Overall, your method is probably better for the reasons of simplified maintanence. The only change I would make to yours is that I would use the x operator in the initialisation rather than iterated pushes.

        my @mirrors = ( ('http://foo.mirror.org') x 3, ('http://www.users-domain.com/proj/foo') x 2, ('http://osdn.dl.sf.net/sourceforge/foo') x 3, ('http://unc.dl.sf.net/sourceforge/foo') x 4, ('http://umn.dl.sf.net/sourceforge/foo') x 4, ('http://heanet.dl.sf.net/sourceforge/foo') x 5, ('http://aleron.dl.sf.net/sourceforge/foo') x 5, );

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found