Generate a "random" number with specific weight
Anyone knows/suggests the logic to get a random number with some specific weights?
A clear example: banner serving, from a banner pool, you get a banner, and that banner has a weight.
One solution I've seen: create an array with the heavier elements have more copies in the array.
So that: a banner with weight 10 and there are 10 other banners with weight 1, the pool banner will have 11 elements, and the banner with weight 10 appears 10 times!
What I want to ask is another solution(or algorithm)? Or is this one the best algorithm?
http://new.123finder.com/ - Helps you find cool domains for FREE & Registers it for only $14.95/yr
http://www.ad-rotator.com/ - Free ad rotating system for small websites - Stop Ctrl-P/Ctrl-C the ad code!
------
Son
richjb posted this at 04:43 — 11th August 2000.
They have: 193 posts
Joined: Feb 2000
Recipe 2.11 of the CGI/PERL Cookbook covers what you want to do...but I'm not sure of how it works, and am not sure if I can post the code.
Richard
[email protected]
Everyone here has a website. It's just that not all are worth posting (Mine! ).
123finder.com posted this at 05:06 — 11th August 2000.
They have: 60 posts
Joined: Aug 2000
I don't have that book, so if you have time, please grasp the basic idea. I really love to know it!!
I don't need the code, since I think I might code it in PHP. The algorithm is most important for me. Thanks for your help in advance!
http://new.123finder.com/ - Helps you find cool domains for FREE & Registers it for only $14.95/yr
http://www.ad-rotator.com/ - Free ad rotating system for small websites - Stop Ctrl-P/Ctrl-C the ad code!
------
Son
richjb posted this at 06:00 — 11th August 2000.
They have: 193 posts
Joined: Feb 2000
To tell the truth, I'm having a lot of problems trying to...don't get me wrong; I'm good at PERL, but as a CGI language, and not as just a regular one. This book takes for granted that I am used to typing ym code right into the interpretuer (sp?), which I'm not. I've been trying to figure this out since you posted your message a few days ago.
You said you didn't want to code but that's the best I could to (I found the info online somewhere, but lost it, otherwise I would point you to that):
sub weight_to_dist {
my %weights = @_;
my %dist = ();
my $total = 0;
my ($key, $weight);
local $_;
foreach (values %weights) {
$total += $_;
}
while ( ($key, $weight) = each %weights ) {
$dist{$key} = $weight/$total;
}
return %dist;
}
sub weighted_rand {
my %dist = @_;
my ($key, $weight);
while (1) {
my $rand = rand;
while ( ($key, $weight) = each %dist ) {
return $key if (rand -= $weight) < 0;
}
}
}
I don't know what to pass to it (I tried several things, none worked), and my best guys is that $key is the rand number it produces. If you figure anything out, please tell me!
Richard
[email protected]
Everyone here has a website. It's just that not all are worth posting (Mine! ).
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.