I'm trying to write a perl script (one which I plan to post in the Cool Uses or Craft area), but I have come accross a problem. I have a hash that has a variable amount of keys, all of which add up to another variable(though this is 100 most of the time).

The function has a reference to the aforementioned hash and an array that contains they keys of the hash
What I need from this is to select from the array an element(or this could be thought of as one key from the hash). The trick is, though, that the values from the hash each contain the probability of that key being chosen out of the variable $amnt. Let's assume that $amnt is 100; if $hash{key} equalled 50, key would have a 50/100, or %50 chance of being chosen. I figured out that if I put this:
int(rand(99)) < 50 ? "a" : "b" that a would have a %50 chance of being chosen(or so I think).

The problem is that I do not know what I would need to do to have this work for many(a variable number of) objects. I came up with some code, but it is incomplete becaus I stopped when I discovered the problem. Here it is, however, for what little worth it may be to you:

sub get_cat { #cat stands for category, by the way my($hash,$amnt) = @_; my $code; for(@$cats) { unless($_ eq $cats->[-1]) { $code .= "int(rand($amnt-1))<$hash->{$_}?$_:" } else { $code .= "$hash->{$_};"; } } ...and I planned to eval $code, but I realized the problem }

I hope I've been clear, but if I haven't I'll try to summarize what my problem is:
I have a hash. I want to choose one key out of the hash to return. However, each key has a chance of getting chosen that is equal to $hash{$key} over $amnt. I do not know enough math to make this work, since I figured that the following code wouldn't do what I expected:
int(rand(99)) < 50 ? "a" : int(rand(99)) < 40 ? "b" : "c";
I realize that there is a %40 of a %50 chance for "b" to be chosen, or so I think, but I want to be able to make the code whereas there is a %50 chance for a, %40 for b, and %10 for c (though this is just an example to explain; I need more general code than this)

In reply to Per cents and Probabilities by dimmesdale

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.