2017-07-19 6 views
0

ListBoxから別のランダムジェネレータを作成しています。 listBoxからランダムに3つのアイテムを選んでTextBoxに表示してもらいたいです。ListBoxからランダムなアイテムを繰り返し選択せずに選択する方法

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]; 

問題が2回選択されることがあります。 例:

のListBox項目:一、二、三、四、五、六

出力:1つは、六、一つ(私はしたくない二回選択された項目「ワン」、)

ありがとうございました。

+0

enumerable.rangeを使用して、0からlist.count - 1までの整数のリストを取得します。シャフリングアルゴリズムをルックアップします。カードのデッキをシャッフルするように。 – 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 'String interpolation" https://docs.microsoft。この '$ 'のようなテキストに変数を追加することができるので、これは{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) ]; 
} 

これはユニークなitemAになり、itemBitemC

+0

@Downvoteは私の投稿を改善することができますのでご説明ください。この解決策は動作します – EpicKip

+0

私はそれをdownvoteする人ではない、私はまだ新しいユーザーだからアップアップすることはできません。ちなみに、回答してくれてありがとう。私は一時的な回避策としてこれを使用しますが、より良い回答があるかもしれません。 – Alfian

+0

@ Alfian私はすでにあなたがdownvoteできなかったことを見ました:)そして、私は本当にあなたがフィードバックなしで自分の投稿を改善することはできませんし、銀色のC#badge-_- – 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つ以上のアイテムをやりたければ、他のアプローチでは少し醜いものになります。

関連する問題