• Shuffle Pseudo Code
    2 replies, posted
Hey, I just started programming and I created a function for shuffling an arrays elements and I cannot figure out how to make Pseudo Code for it. My pseudo code is on this link but I don't think it is that great neither is it efficient. [IMG]http://i.imgur.com/sGiHKwZ.png[/IMG] My code is right here: [CODE] static void Shuffle() { //Stores as a random generated number from 0 Random shuffleCards = new Random(); //Loops the nested loop 1000 times to shuffle the cards properly for (int i = 0; i <= 1000; i++) { //Shuffles the stored cardDeck elements into different cardDeck elements 52 times for (int shuffleNum = cardDeck.Length; shuffleNum > 0; shuffleNum--) { //Assigns cardType a randomly generated number from 0 to shuffleNum value. shuffledCardValue = shuffleCards.Next(shuffleNum); //Assigns newCard to an element from cardDeck so it doesn't lose its data when assigned to a new cardDeck element. shuffledCard = cardDeck[shuffledCardValue]; //Assigns cardDeck array with a generated number element to the same cardDeck array with an element of shuffleNum - 1. cardDeck[shuffledCardValue] = cardDeck[shuffleNum - 1]; //Assigns cardDeck array with current shuffleNum - 1 to newCard to shuffle the two elements. cardDeck[shuffleNum - 1] = shuffledCard; } } } [/CODE] If anyone can please help me out that would be amazing! :)
Since you're not actually handling a real deck of cards, the random number generation should be more than random enough to shuffle the array sufficiently for any reasonable purpose in one pass. Your pseudo-code is also a little hard to read but that may just be me. It'd probably help your readability a lot if you saved the value you're taking from the array to a separate temporary variable. I'd probably go for something closer to: [code] For i = 0 to deckSize, step 1: shuffleNum = randomInteger(between 0 and deckSize inclusive) temp = deck[i] deck[i] = deck[shuffleNum] deck[shuffleNum] = temp End For //So here we're grabbing the current card and switching it with another random card in the deck via a separate variable called temp. You're doing this to every card so you're going to have a pretty well shuffled deck at the end of it. [/code]
Thank you so much. I really appreciate it.
Sorry, you need to Log In to post a reply to this thread.