2017-07-19 83 views
0

我從ListBox創建另一個隨機生成器。我希望他們隨機從listBox中挑選3件物品,然後在TextBox上展示它。如何從列表框中選擇不重複的隨機項目

Random random = new Random(); 
int a = random.Next(0, listBox1.Items.Count); 
listBox1.SelectedItem = listBox1.Items[a]; 
int b = random.Next(0, listBox1.Items.Count); 
listBox1.SelectedItem = listBox1.Items[b]; 
int c = random.Next(0, listBox1.Items.Count); 
listBox1.SelectedItem = listBox1.Items[c]; 
listBox1.Select(); 
textBox1.Text = listBox1.Items[a] + ", " + listBox1.Items[b] + ", " + listBox1.Items[c]; 

該問題有時會被選中兩次。 例子:

ListBox的項目:一,二,三,四,五,六

輸出:一,六,一(選擇兩次項目「一」,這是我不希望)

謝謝。

+0

使用enumerable.range獲取從0到list.count的整數列表 - 查找混洗算法。就像洗牌一樣。 – Derek

+0

[生成隨機,唯一值C#]的可能的重複(https://stackoverflow.com/questions/14473321/generating-random-unique-values-c-sharp) – EpicKip

回答

0

我會改變你的邏輯一樣這樣的:

  1. 得到3張隨機數
  2. 從次選擇的值e ListBox

代碼可能看起來像。當電流已被選中時,此代碼將獲得一個新的隨機數並將其存儲在List<int>中。

Random random = new Random(); 
List<int> numbers = new List<int>(); 
for (int i = 0; i < 3; i++) 
{ 
    int number = random.Next(0, listBox1.Items.Count); 
    while (numbers.Contains(number)) 
    { 
     number = random.Next(0, listBox1.Items.Count); 
    } 
    numbers.Add(number); 
} 

然後

textBox1.Text = $"{listBox1.Items[numbers[0]]}, {listBox1.Items[numbers[1]]}, {listBox1.Items[numbers[2]]}"; 

如果你在你的ListBox少於3項,這將在一個無限循環的結束,意識到這一點。

+0

謝謝。順便說一下,在'textBox1.Text'中有什麼'$'? – Alfian

+0

@Alfian'字符串插值'https://docs.microsoft。com/en-us/dotnet/csharp/language-reference/keywords/interpolated-strings thats所以你可以在文本中添加變量,就像這個'$「這是文本這是{variableName}」' – EpicKip

+0

@Alfian [ string.format](https://msdn.microsoft.com/de-de/library/system.string.format(v = vs.110).aspx)您的歡迎和它的工作時間,請接受答案;) –

0

你可以把它放在一個while循環,只有出關時它們不匹配:

Random random = new Random(); 
int listBoxItemCount = listBox1.Items.Count; 
var itemA = listBox1.Items[random.Next(listBoxItemCount)]; 
var itemB = listBox1.Items[random.Next(listBoxItemCount)]; 
var itemC = listBox1.Items[random.Next(listBoxItemCount)]; 

while(itemA == itemB || itemA == itemC || itemB == itemC)//While any pair matches 
{ 
    itemB = listBox1.Items[ random.Next(listBoxItemCount) ]; 
    itemC = listBox1.Items[ random.Next(listBoxItemCount) ]; 
} 

這將導致一個獨特的itemAitemBitemC

+0

@Downvote解釋請,所以我可以改進我的帖子。這個解決方案確實有效 – EpicKip

+0

我並不是那種低調的人,因爲我還是新用戶,所以我甚至無法贊成。順便說一句,謝謝你的回答,它的工作原理。我會用它作爲暫時的解決方法,因爲可能會有更好的答案。 – Alfian

+0

@Alfian我已經看到你不能downvote :),我真的不介意投票本身,只是我不能改善我的職位,沒有反饋,並失去了銀c#徽章的進展-_- – EpicKip

0

參見:Randomize a List<T>

我修改,並提出了.NET小提琴:https://dotnetfiddle.net/a7RJbm

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     List<int> list = Enumerable.Range(0, 6).ToList(); 
     list.Shuffle(); 
     int a = list[0]; 
     int b = list[1]; 
     int c = list[2]; 
     Console.WriteLine(a); 
     Console.WriteLine(b); 
     Console.WriteLine(c); 
    }  

} 

public static class ListShuffler 
{ 
    private static Random rng = new Random(); 
    public static void Shuffle<T>(this IList<T> list) 
    { 
     int n = list.Count; 
     while (n > 1) 
     { 
      n--; 
      int k = rng.Next(n + 1); 
      T value = list[k]; 
      list[k] = list[n]; 
      list[n] = value; 
     } 
    } 
} 

洗牌的好處是,你可以隨意瀏覽所有在列表中的項目。如果你想要做3個以上的項目,其他一些方法會變得有點難看。

相關問題