C# :: Aufgabe #9 :: Lösung #7
10 Lösungen

#9
Listeninhalt zufällig anordnen
Anfänger - C#
von pocki
- 27.08.2012 um 20:49 Uhr
Erstelle eine Methode welche die Einträge (vom beliebigen Typ) einer Liste in zufälliger Reihenfolge anordnet. Die zufällige Anordnung soll in einer neu erstellten Liste zurückgegeben werden.
#7

von Shirkova (190 Punkte)
- 03.07.2016 um 23:28 Uhr

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { List<int> list = new List<int>() { 1,2,3,4,5,6,7,8,9,10 }; List<int> randomizedList = Fisher_Yates_Shuffle<int>(list); foreach (var i in randomizedList) { Console.Write("{0} ", i); } Console.Read(); } static List<T> Fisher_Yates_Shuffle<T>(List<T> list) { //Check Google for Fisher-Yates shuffle to get further information about this algorithm. T temp; Random r = new Random(); int n = list.Count; while (n > 1) { int x = r.Next(n); n--; temp = list[n]; list[n] = list[x]; list[x] = temp; } return list; } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1