4択クイズの回答の並びをランダムにする

すごい具体的なタイトルを書いたけど、例えば以下のような紐付けを、どうやったらランダム化出来るか考えてみたって話。

[C#]
//4択クイズの回答ボタンセット
button1.Text = nowQ["Choice1"].ToString();
button2.Text = nowQ["Choice2"].ToString();
button3.Text = nowQ["Choice3"].ToString();
button4.Text = nowQ["Choice4"].ToString();

これを、

[C#]
//4択クイズの回答ボタンセット
button1.Text = nowQ["Choice4"].ToString();
button2.Text = nowQ["Choice1"].ToString();
button3.Text = nowQ["Choice3"].ToString();
button4.Text = nowQ["Choice2"].ToString();

結果的にランダムに紐付けたい。

思いつく限りスムーズに

とか考えてたんだけど…

[C#]
//4択クイズの回答ボタンセット
var choices = new List(){ "Choice1", "Choice2", "Choice3", "Choice4" };
Random random = new Random();
int index = random.Next(choices.Count);
button1.Text = nowQ[choices[index]].ToString();
choices.RemoveAt(index);
index = random.Next(choices.Count);
button2.Text = nowQ[choices[index]].ToString();
choices.RemoveAt(index);
index = random.Next(choices.Count);
button3.Text = nowQ[choices[index]].ToString();
choices.RemoveAt(index);
index = random.Next(choices.Count);
button4.Text = nowQ[choices[index]].ToString();

なんか違う…

クソみたいに見難いコードになってしまった。

誰か良い感じのやり方を教えてください。

投稿者: Output48

中学生の時に初めてHTMLに触れてからホームページ制作を独学で始める。 ベンチャー企業の営業、大手企業のPG・SEを経て、独立。 現在はとある企業のCTOと、変な名前の会社の社長をしてる。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください