mardi 26 décembre 2017

Pick random numbers from List based on specified condition C#

I'm trying to generate a unique random numbers from the list. Here user will input the followings:

  1. Number of random numbers required
  2. Number C1, C2 and C3 items.

Example: 10 Random numbers with C1-5, C2-4, and C3-1.

So based on these conditions a random number list need to generate.

My list looks like this

1   C1
2   C2
3   C3
4   C3
5   C2
6   C1
7   C2
8   C3
9   C1
10  C2
11  C1
12  C3
13  C3
14  C1
15  C2
16  C2
17  C4
18  C3
19  C4
20  C4
21  C4
22  C1
23  C2
24  C3
25  C4
26  C3
27  C4

My code looks like this:

protected void BtnGenerate_Click(object sender, EventArgs e)
{
            List<string> labels; // Holds all Labels (unique)
            List<string> values; // Holds all numbers of labels
            Random r = new Random();
            StringBuilder sb=new StringBuilder(100);

            sb.Clear();
            var randoms = values.OrderBy(x => r.Next()).Take(Convert.ToInt16(txtNumberOfRandomNumbers.Text));
            foreach (var item in randoms)
            {
                sb.Append(item.ToString() + ",");
            }

            lblRandomNumbers.Text = sb.ToString().Remove(sb.ToString().LastIndexOf(","));
}

I'm stuck with how to add these conditions to Random function. Please help me on this.




Aucun commentaire:

Enregistrer un commentaire