一个洗牌方法 麻烦高手来看看我这问题。。。
照着例子 弄了个洗牌的方法
public void shuffle() { Card[] newDeck = new Card[52]; bool[] assigned = new bool[52]; Random sourceGen = new Random(); for (int i = 0; i < 52; i++) { int destCard = 0; bool foundCard = false; while (foundCard == false) { destCard = sourceGen.Next(52); if (assigned[destCard] == false) foundCard = true; } assigned[destCard] = true; newDeck[destCard] = cards[i]; } newDeck.CopyTo(cards, 0); }
List<int> oldcard = new List<int>(){1,2,3,4,5,6....};List<int> newcard = new List<int>();Random r = new Random();while (oldcard.Count > 0){ n = r.Next(0,oldcard.Count); newcard.Add(oldcard[n]); oldcard.RemoveAt(n);}
[解决办法]
int[] result = Enumerable.Range(1, 52) .Select(x => new { r = Guid.NewGuid().ToString(), v = x }) .ToList() .OrderBy(x => x.r) .Select(x => x.v) .ToArray();