Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: weighted biased dice roll (bugs)

by tye (Sage)
on Aug 05, 2014 at 17:41 UTC ( [id://1096325]=note: print w/replies, xml ) Need Help??


in reply to weighted biased dice roll

That code is wrong in three ways, as far as I can tell.

The biggest problem is that you need to pass the sum of all of the weights to rand(). If the weights happen to all add up to 1, then rand() with no arguments is fine. (Update: The original code didn't have weights that added up to 1.)

You don't really need the outer while(1) loop. Instead, just return the last item even if subtracting its weight from $rand doesn't quite get you to 0.

Using each means you need to reset the iterator, preferably both before and after.

No, none of that is necessarily going to help you understand the code. I might take a stab at explaining at a later time, if nobody else does. :)

- tye        

Replies are listed 'Best First'.
Re^2: weighted biased dice roll (bugs)
by ikegami (Patriarch) on Aug 05, 2014 at 17:49 UTC
    Each call of the function iterates over a new hash, so the iterator doesn't need resetting.
Re^2: weighted biased dice roll (bugs)
by sys0p (Novice) on Aug 05, 2014 at 17:47 UTC
    I have figured it out, thanks anyway!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-16 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found