I am looking to pick 1 person from a list of people where each item in the list has a certain "weighting" to it. Let's assume the Person class has the necessary constructor.  public class Person {     public string Name { get; set; }     public float Weighting { get; set; } }  public List&lt;Person&gt; People = new List&lt;Person&gt;();  People.Add(new Person("Tim", 1.0)); People.Add(new Person("John", 2.0)); People.Add(new Person("Michael", 4.0));   Now, I want to choose a person randomly from this list. But on average I want to pick Michael 4 times more often than Tim. And I want to pick John half (2/4) as often as Michael. And of course, I want to pick Michael double as often as John.  Does that make any sense?  I already have code in place to select people based on percentages. Wouldn't it work if I just multiplied the %chance of them by the weighting provided in this example?  Also, my current system only works with chances of up to 100%, nothing above. Any advice on how to overcome this limitation? I would probably have to scale every chance according to the largest factor in the list?  public static bool Hit(double pct) {     if (rand == null)         rand = new Random();      return rand.NextDouble() * 100 &lt;= pct; }   Or am I missing something?
16473
Consider the class below that represents a Broker:  public class Broker {     public string Name = string.Empty;     public int Weight = 0;      public Broker(string n, int w)     {         this.Name = n;         this.Weight = w;     } }   I'd like to randomly select a Broker from an array, taking into account their weights.  What do you think of the code below?  class Program     {         private static Random _rnd = new Random();          public static Broker GetBroker(List&lt;Broker&gt; brokers, int totalWeight)         {             // totalWeight is the sum of all brokers' weight              int randomNumber = _rnd.Next(0, totalWeight);              Broker selectedBroker = null;             foreach (Broker broker in brokers)             {                 if (randomNumber &lt;= broker.Weight)                 {                     selectedBroker = broker;                     break;                 }                  randomNumber = randomNumber - broker.Weight;             }              return selectedBroker;         }           static void Main(string[] args)         {             List&lt;Broker&gt; brokers = new List&lt;Broker&gt;();             brokers.Add(new Broker("A", 10));             brokers.Add(new Broker("B", 20));             brokers.Add(new Broker("C", 20));             brokers.Add(new Broker("D", 10));              // total the weigth             int totalWeight = 0;             foreach (Broker broker in brokers)             {                 totalWeight += broker.Weight;             }              while (true)             {                 Dictionary&lt;string, int&gt; result = new Dictionary&lt;string, int&gt;();                  Broker selectedBroker = null;                  for (int i = 0; i &lt; 1000; i++)                 {                     selectedBroker = GetBroker(brokers, totalWeight);                     if (selectedBroker != null)                     {                         if (result.ContainsKey(selectedBroker.Name))                         {                             result[selectedBroker.Name] = result[selectedBroker.Name] + 1;                         }                         else                         {                             result.Add(selectedBroker.Name, 1);                         }                     }                 }                   Console.WriteLine("A\t\t" + result["A"]);                 Console.WriteLine("B\t\t" + result["B"]);                 Console.WriteLine("C\t\t" + result["C"]);                 Console.WriteLine("D\t\t" + result["D"]);                  result.Clear();                 Console.WriteLine();                 Console.ReadLine();             }         }     }   I'm not so confident. When I run this, Broker A always gets more hits than Broker D, and they have the same weight.  Is there a more accurate algorithm?  Thanks!
83100
A subset of $\{1, 2, 3, \dots, 20\}$ containing 10 elements is chosen at random. The largest element of this subset is noted. Find the expected value of the largest element.  I tried to solve this problem by finding the probability of each element being the largest one, but I got confused and would like some help. :)
28043
There is a party with 20 people, and everyone writes their name down   on a piece of paper and puts it into a bag. The bag is mixed up, and   each person draws one piece of paper. If you draw the name of someone   else, you are considered to be in his "group". What is the expected   number of groups after everyone draws?   So basically if we have a loop where each person draws someone else's name, and the last person draws the first person in that list's name, we have a group. Not quite sure how to approach this problem.  Thanks for any help.
10845
Suppose there are $m$ red balls and $n$ blue balls in an urn. We randomly choose $p:m&lt;p&lt;n$ balls uniformly from the urn. What is the probability that exactly $q$ red balls are chosen?  Note:- Normally the answer would be $\frac{{m}\choose{q}}{{m+n}\choose{q}}$. However, since the number of balls that are chosen are provided, it is confusing me out. Any hints for the answer will also be appreciated.
19599
What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I'd like to create a new Array with the same strings but in a random order.  Please include a C# example in your answer.
9038
Randomly break a stick (or a piece of dry spaghetti, etc.) in two places, forming three pieces.  The probability that these three pieces can form a triangle is $\frac14$ (coordinatize the stick form $0$ to $1$, call the breaking points $x$ and $y$, consider the unit square of the coordinate plane, shade the areas that satisfy the triangle inequality edit: see comments on the question, below, for a better explanation of this).  The other day in class*, my professor was demonstrating how to do a Monte Carlo simulation of this problem on a calculator and wrote a program that, for each trial did the following:   Pick a random number $x$ between $0$ and $1$.  This is the first side length. Pick a random number $y$ between $0$ and $1 - x$ (the remaning part of the stick).  This is the second side length. The third side length is $1 - x - y$. Test if the three side lengths satisfy the triangle inequality (in all three permutations).   He ran around $1000$ trials and was getting $0.19$, which he said was probably just random-chance error off $0.25$, but every time the program was run, no matter who's calculator we used, the result was around $0.19$.  What's wrong with the simulation method?  What is the theoretical answer to the problem actually being simulated?  (* the other day was more than $10$ years ago)
115847
Number of ways to select k people from a round table of n people so that no two selected people sit next to each other.    I tried the followings.  k = 1, Number of ways = n  k = 2, Number of ways = n * (n-3)  k = 3, for the first person, we have n choices        for the second person: Case 1: we can select the person sitting next next to the first person, so we have 2 choices for the second person, and the third person we have n-5 choices. Case 2: we select the remaining people other than those in case 1, so we have n-5 choices for the second person, and the third person we have n-6 choices. Then, the number of ways = Case 1 + Case 2 = n*2*(n-5) + n*(n-5)*(n-6)  For k >= 4, it seems that there are many cases for each step, so is there any better way to generalize the formula to select k people?
100540
This is a variation of a famous problem, that I have heard before but I have never seen it in this format before.      $N$ Guests arrive at a party, each wearing a hat. We collect all the hats and then randomly redistribute the hats, giving each person one of the $N$ has randomly. Letting $X_N$ be the number of hats that are matched correctly, what is the $PMF$ of $X_N$? I.e.    $P(X_N=k)=$   The common variations of this problem that I have seen usually involve the probability that no one gets the correct hat or as $N$ approaches $\infty$. I am struggling to identify a distribution that would result in any type of general formula for $PMF$ of this random variable. Has anyone seen this before?
45005
The number of children in a family is a random variable $X$, where    $ \mathbb E(X) = 1.8 $ and $Var(X) = 0.36 $. If we randomly choose a child, which is the expected number of its siblings?      (it is given that the answer $&gt; 0.8$)   Let the number of a child's siblings be a random variable $Y$, so we are looking for $ \mathbb E(Y) $.  If found that,  $$ \mathbb E(Y) = \sum_{k=0}^{\infty}k \; \mathbb P(Y=k) = \sum_{k=0}^{\infty}k \; \mathbb P(X=k+1) = $$ $$ = \sum_{k=0}^{\infty}k \; \mathbb P(X=k)-\sum_{k=0}^{\infty}\mathbb P(X=k) +\mathbb P(X=0)= \mathbb E(X)-1+\mathbb P(X=0) $$ which is indeed greater than $0.8$, if  $\; \mathbb P(X=0)&gt;0$.  But, I don't know how to make use of the variance to find the final answer.  Thank you in advance
39932
Given $n$ items with weight $w_n$ each -- what is the probability that item $i$ is chosen in a $k$-out-of-$n$ "weighted random sampling without replacement" experiment? Can a closed-form solution that depends only on $w_i / w_\cdot$ be derived ($w_\cdot = \sum_j w_j$.)?   EDIT: A solution that depends only on $w_i / w_\cdot$ is impossible. Assume $n=3$, $i=1$, $w_1 = 1$, and two cases: (1) $w_2 = 1, w_3 = 1 + \varepsilon$, (2) $w_2 = 2, w_3 = \varepsilon &gt; 0$. In case (1) the probability is almost $2/3$, in case (2) it is almost $1$, but in both cases $w_1 = 1$ and $w_\cdot = 3 + \varepsilon$.  What I have tried so far to solve the problem:  Let $P^n_k(w, i)$ be the probability that item $i$ is chosen in an $k$-out-of-$n$ experiment with weight vector $w$. In the first draw, the item is chosen with probability $w_i / w_\cdot$. Otherwise, we are looking for the probability to choose this item in a $k-1$-out-of-$n-1$ experiment, with the same weight vector except for the item that has been selected. Hence:  $$ P^n_k(w, i) = w_i / w_\cdot + \sum_{j \neq i} w_j / w_\cdot \cdot P^{n-1}_{k-1}(w - j, i) $$ $$ = w_\cdot^{-1} \left( w_i + \sum_{j \neq i} w_j \cdot P^{n-1}_{k-1}(w - j, i) \right) $$  with $w-j$ being "the vector $w$ without the $j$th element".  How to solve this recurrence relation? (If it is correct at all...)
44353
I was trying to answer      Find the probability of getting a full house from a $52$ card deck. That is, find the probability of picking a pair of cards with the same rank (face value), and a triple with equal rank (different from the rank of the pair of course).   My idea was this:  Take one rank. The number of ways you can get a pair from a rank is $C(4, 2)$.   Take another rank. The number of ways you can get a triple from that rank is $C(4, 3)$.   Therefore, the number of ways you can get a full house from just $2$ ranks is $C(4, 2) \cdot C(4, 3)$. But there are $13$ different ranks, and the number of ways you can select $2$ ranks out of $13$ is $C(13, 2)$. For each of these pairs of ranks, you can have $C(4, 2) \cdot C(4, 3)$ full houses. Therefore the number of possible full houses is $C(4, 2) \cdot C(4, 3) \cdot C(13, 2)$, and the probability of selecting a full house is  $\dfrac {C(4, 2) \cdot C(4, 3) \cdot C(13, 2)}{C(52, 5)} \approx 0.000720288$  However, this is way lower than the actual answer of $.00144$. Where did I go wrong here?
71247
List&lt;String&gt; nameList = new List&lt;String&gt;(); DropDownList ddl = new DropDownList();   List is populated here, then sorted:  nameList.Sort();   Now I need to drop it into the dropdownlist, which is where I'm having issues (using foreach):  foreach (string name in nameList){     ddl.Items.Add(new ListItem(nameList[name].ToString())); }   No workie - any suggestions? It's giving me compile errors:  Error - The best overloaded method match for 'System.Collections.Generic.List&lt;string&gt;.this[int]' has some invalid arguments   Error - Argument '1': cannot convert from 'string' to 'int'
119910
I'm using .NET 3.5 and would like to be able to obtain every *n*th item from a List.  I'm not bothered as to whether it's achieved using a lambda expression or LINQ.  Edit  Looks like this question provoked quite a lot of debate (which is a good thing, right?).  The main thing I've learnt is that when you think you know every way to do something (even as simple as this), think again!
40567
I'm working on a project where i find i'm checking for the following in many, many places:  if(item.Rate == 0 || item.Rate == null) { }   more as a curiousity than anything, what's the best way to check for both cases?  I've added a helper method which is:  public static bool nz(object obj) {     var parsedInt = 0;     var parsed = int.TryParse(obj.ToString(), out parsedInt);     return IsNull(obj) || (parsed &amp;&amp; parsedInt == 0); }   Is there a better way?
93387
"$n$ music lovers have reserved seats in a theater containing a total of $n+k$ seats ($k$ seats are unassigned). The first person who enters the theater, however, lost his seat assignment and chooses a seat at random. Subsequently, people enter the theater one at a time and sit in their assigned seat unless it is already occupied. If it is, they choose a seat at random from the remaining empty seats. What is the probability that person $n$, the last person to enter the theater, finds their seat already occupied?"  I could do this problem for small specific values of $n$ and $k$ but as they grow the expressions seem to get really messy really quick with no discernible pattern. How would one solve this problem?
36086
I'm using C# on Framework 3.5.  I'm looking to quickly sort a Generic List&lt;T&gt;.  For the sake of this example, let's say I have a List of a Person type with a property of lastname.  How would I sort this List using a lambda expression?  List&lt;Person&gt; people = PopulateList(); people.OrderBy(???? =&gt; ?????)
100926
I was looking at this website  and I read the explanation for how to calculate the probability of getting a full house.  To me, the logic basically looked like you figure out the number of possible ranks and multiply by the number of ways to choose the cards from that given rank.  In other words, for a full house $P=$ $$\frac{{13\choose1}{4\choose3}{12\choose1}{4\choose2}}{52\choose5}$$  Following this logic, I tried to calculate the probability of getting two pair.  My (incorrect) logic was that there are 13 possible ranks for the first pair and $4\choose2$ ways to choose two cards from that rank, 12 possible ranks for the second pair and $4\choose2$ ways to choose two cards from that rank, and 11 possible ranks for last card and $4\choose1$ ways to choose a card from that rank.    So I tried $P=$ $$\frac{{13\choose1}{4\choose2}{12\choose1}{4\choose2}{11\choose1}{4\choose1}}{52\choose5}$$  Obviously my solution was incorrect.  I read explanation and the correct answer is $P=$ $$\frac{{13\choose2}{4\choose2}{4\choose2}{11\choose1}{4\choose1}}{52\choose5}$$  I'm still a bit fuzzy on where I went wrong though.  Can anyone help me understand this problem a little better?  Thank you very much for your help.