Picking weighed enumerable values at random I have a number of enumerables, and I want to randomly pick one value from one of the enumerables so that each value has a certain weight in the randomisation process. This is simplification of my current code:

private enum ActionPlan //Just an example, there can be many different enums
{
    DoNothing,
    Eat,
    StealFoodCovertly,
    AttackAndSteal,
}

private ActionPlan PickPlan()
{
    double[] odds = new double[Enum.GetNames(typeof(ActionPlan)).Length];
    odds[(int)ActionPlan.DoNothing] = 2.5;
    odds[(int)ActionPlan.Eat] = 3;
    odds[(int)ActionPlan.StealFoodCovertly] = 1;
    odds[(int)ActionPlan.AttackAndSteal] = 0.5;

    ActionPlan plan = (ActionPlan)PickOption(odds);
    return plan;
}

private int PickOption(double[] odds)
{
    double totalOdds = 0;
    foreach (double odd in odds)
    {
        totalOdds += odd;
    }
    double pick = rand.NextDouble() * totalOdds;
    for (int i = 0; i &lt; odds.Length; ++i)
    {
        pick -= odds[i];
        if (pick &lt;= 0)
        {
            return i;
        }
    }
    return odds.Length - 1; // In case there are some precision shenanigans
}


I can't help but to think I'm trying to force a square peg into a round hole with all the casting going on. Is there some better way to accomplish this functionality? The idea was that the PickOption method can handle any enumerable and odds I throw at it. 

I understand this question is rather open ended, but it doesn't hurt to ask, I suppose.
You're right that all the casting is a sign that there's better way.  Rather than using the int value of an ActionPlan as the index to an array, you should use a structure that's designed to store two values in the first place.  There's a couple of alternatives here.  

My first instinct is to use a Dictionary&lt;ActionPlan, double&gt;.  That most explicitly matches the concept of what you're trying to do (each A has a value B), but it's not actually the best option.  You're not going to be looking up the value based on the key, the order of the keys may change which could lead to hard-to-reproduce bugs, and it's less memory-efficient.

Instead, I'd use a List&lt;KeyValuePair&lt;ActionPlan, double&gt;&gt;, List&lt;Tuple&lt;ActionPlan, double&gt;&gt;, or (in C# 7) List&lt;(ActionPlan, double)&gt;.  They're all effectively the same thing in this case, and best convey the concept "here is a pair of related values" without implying the A->B relationship of the Dictionary.  (Note that you can also use arrays instead of List&lt;&gt;, like I do below).  To make PickOption() handle this, you would need to make it generic, so that you can pass any arbitrary type in and get that same type out.   

Here's what the code would look like using C# 7's ValueTuples: 

private ActionPlan PickPlan()
{
    (ActionPlan, double)[] odds = new[] {
        (ActionPlan.DoNothing, 2.5),
        (ActionPlan.Eat, 3),
        (ActionPlan.StealFoodCovertly, 1),
        (ActionPlan.AttackAndSteal, 0.5),
    };

    ActionPlan plan = PickOption(odds);
    return plan;
}

private T PickOption&lt;T&gt;((T, double)[] odds)
{
    double totalOdds = 0;
    foreach (var odd in odds)
    {
        totalOdds += odd.Item2;
    }
    double pick = rand.NextDouble() * totalOdds;

    for (int i = 0; i &lt; odds.Length; ++i)
    {
        pick -= odds[i].Item2;
        if (pick &lt;= 0)
        {
            return odds[i].Item1;
        }
    }
    return odds.Last().Item1; // In case there are some precision shenanigans
}


or better: 

private T PickOption&lt;T&gt;((T Action, double Weight)[] odds)
{
    double totalOdds = 0;
    foreach (var odd in odds)
    {
        totalOdds += odd.Weight;
    }
    double pick = rand.NextDouble() * totalOdds;

    foreach (var odd in odds)
    {
        pick -= odd.Weight;
        if (pick &lt;= 0) return odd.Action;
    }
    return odds.Last().Action; // In case there are some precision shenanigans
}




There are other improvements that can be made here, too, depending on how much you simplified for this question, and how far you want to go.  For a few examples: 


Both methods can be made static, because they don't depend on anything specific to whatever class they're in.
Rather than redefining the odds each time that PickPlan() is called, you can create a static readonly (ActionPlan, double)[] odds and populate it once.  This also lets you do that right next to where your ActionPlan enum is defined, so that when you add a new enum value, the odds are right there.
If you have other properties associated with an ActionPlan, you may want to consider a class to consolidate them all, rather than let each place they're used keep track.