I am making a card game. I need to shuffle the cards. I made a shuffler but it looks like its not always random. It seems like 0 is always close to the bottom. I remember in VB you had to call Randomize() before it was true random. I can't find that function anymore.
Code:public class CardShuffler { private const int CARDS_IN_DECK = 52; public List<int> Shuffle() { List<int> totalCards = new List<int>(); List<int> shuffledCards = new List<int>(); // Populate totalCards with numbers 0-51 (total of 52 numbers) for (int i = 0; i != CARDS_IN_DECK; i++) { totalCards.Add(i); } Random rnd = new Random(); while (totalCards.Count != 0) { // Get a random number, <= the number of items left in totalCards. int currentCard = rnd.Next(totalCards.Count); // Use the random number to pull the item (card) from totalCards, // and insert it into shuffledCards at the next index. shuffledCards.Add(totalCards[currentCard]); // Remove the item (card) from totalCards, so it is not used again. totalCards.RemoveAt(currentCard); } return shuffledCards; } }Thanks.Code:CardShuffler cs = new CardShuffler(); List<int> ar = cs.Shuffle(); for (int i = 0; i < ar.Count; i++) { listBox1.Items.Add(ar[i]); }![]()




Reply With Quote