私は、ループするたびにランダムに正しい配列の9番目の要素を選ぶループを作っていました。配列の特定の部分をランダムにループする方法はありますか?
合計で99個の要素がループ内にあります。
各9番目の要素は同じです(%9)。私は残りの90をアプリケーションを開くたびにランダムにループさせる必要があります。私はリストに私の要素を追加した後、その
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
Random rnd = new Random();
string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();
string repeatNumber = "";
List<string> animals = new List<string>();
for (int i = 1; i < 100; i++)
{
if (i == 9)
{
repeatNumber = randomingArray[i % randomingArray.Length];
animals.Add(repeatNumber);
}
else if ((i % 9) == 0)
{
animals.Add(repeatNumber);
}
else
{
// random animals.Add(); <-- it should loop randomly here.
}
ItemsControl1.ItemsSource = animals;
}
}
}
との結合を介して、XAMLにそのリストを送信します。
は私のコードを確認してください。
コードを書く時間がありませんが、この問題の典型的な解決方法は、90個の残りの「ランダム」アイテムを別の配列またはリストにコピーし、[_shuffle_](https://stackoverflow.com/質問/ 273313/randomize-a-listt)を使用してください。 –
*具体的な問題を明確にしたり、詳細を追加して必要なものを正確に強調してください。現在書かれているとおり、あなたが求めていることを正確に伝えるのは難しいです。* –
シャッフリングについては、https://stackoverflow.com/questions/1651619/optimal-linq-query-to-get-a-random-sub-コレクションシャッフル/ 1653204#1653204 –