A Tor client generates the desc_id for the v2 hidden service it would like to query. It did selects the 3 HSDIR from the router list which are found directly after the position of the desc_id when the routers are sorted by their identity digest (hash of their onion public key). I took me quite a while to figure out exactly how this works from the relatively sparse info in the rend-spec.txt.
I just put up a Gist with the python implementation of desc_id calculation. It should be pretty straightforward using STEM to sort list of OR's with the HsDir flag by identity_digest and determine which 6 are responsible at the current time.
https://gist.github.com/DonnchaC/5451637