-1

I have a

List<string> notizen = new List<string>();

I want to choose a random entry from it but this entry should not be repeated until all entries of notizen have been shown.

Application looks like this:

String is on the screen, you tap on it, another text is on the screen. I want to randomly go through the notizen list without having double entries until all entries have been shown, then it starts with a new randomized version of notizen.

notizen could be randomized itself, no temporary list necessary. But I found LINQ to not exist in monodroid.

4

1 に答える 1

0

のフィッシャー・イェーツアルゴリズムを使用して、リストをシャッフルできますO(n)。イテレータが n に等しくなったら、2 回目のシャッフルを実行します。

ウィキペディアで提示されているように、その擬似コードは次のとおりです。

To shuffle an array a of n elements (indices 0..n-1):
  for i from n − 1 downto 1 do
   j ← random integer with 0 ≤ j ≤ i
   exchange a[j] and a[i]

次のような拡張メソッドを作成することもできます。

    public static Random rand = new Random();

    public static List<T> Shuffle<T>(this List<T> original)
    {
        List<T> lst = new List<T>(original);
        for (int i = lst.Count - 1; i >= 1; i--)
        {
            int j = rand.Next(0, i + 1);
            T tmp = lst[j];
            lst[j] = lst[i];
            lst[i] = tmp;
        }
        return lst;
    }

シャッフルされたリストを生成できるように

var shuffled = notizen.Shuffle();
于 2013-08-31T22:13:44.893 に答える