Sure, that's quite simple to do.
Just take the sum of both dominance values and take a random.randrange()
value of these.
If that value is lower than the dominance value of parent A, you picked that parent's trait, otherwise it's parent B's trait you picked:
import random
if random.randrange(parentA.dominance + parentB.dominance) < parentA.dominance:
trait = parentA.trait
else:
trait = parentB.trait
In other words, it's a very simple weighted random selection between two options.
For your specific example, the sum of the dominances is 9, so the randrange()
value is one between 0 and 8; if 0 or 1 is picked, parent A's trait is selected, if 2, 3, 4, 5, 6, 7 or 8 is picked then parent B's trait is selected instead.
If instead you are talking about picking a trait on a range from parent A's trait value up to and including parent B's trait value where traits are treated as a range, then your dominance values are used to 'pull' a trait value towards one or the other parent.
It basically comes down to a tug-of-war between the parents. With balanced dominance, the trait picked would, on average, come down to the average value of the two traits. But with one parent dominating over the other, the trait value picked 'shifts' towards the dominating parent.
This translates in a chance that the values below the midpoint are picked for parent A have a chance of parentA.dominance in (parentA.dominance + parentB.dominance), while the values above the midpoint have a chance of parentB.dominance in (parentA.dominance + parentB.dominance) of being picked.
import random
pick = random.random()
# sort parents by trait; smallest trait first
parents = sorted((parentA, parentB), key=attrgetter('trait'))
average = (parents[0].trait + parents[1].trait) / 2.0
weights = []
slots = (parents[1].trait - parents[0].trait + 1.0) * (parents[0].dominance + parents[1].dominance)
for i in range(parents[0].trait, parents[1].trait + 1):
if i <= average:
weights.append(sum(weights) + (parents[0].dominance / slots))
else:
weights.append(sum(weights) + (parents[1].dominance / slots))
if weights[-1] > pick:
return i
A quick demo picking traits between parent A and parent B using the above calculation:
>>> for i in range(40): print pickTrait(),
...
9 9 9 10 6 9 8 10 7 9 9 8 8 8 9 8 9 9 7 5 8 8 9 5 9 9 10 10 10 10 5 5 9 9 8 9 10 7 8 9