Welcome to the Monastery | |
PerlMonks |
Per cents and Probabilitiesby dimmesdale (Friar) |
on Jun 25, 2001 at 01:58 UTC ( [id://91123]=perlquestion: print w/replies, xml ) | Need Help?? |
dimmesdale has asked for the wisdom of the Perl Monks concerning the following question:
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
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:
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: 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)
Back to
Seekers of Perl Wisdom
|
|