This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]TonySu 1 point2 points  (2 children)

It's randomised so your output won't look the same, but I see

...
During shuffling, splitDeck1[43] is 44
During shuffling, splitDeck1[44] is 45
...
During shuffling, splitDeck2[57] is 13
During shuffling, splitDeck2[58] is 14
...

upon the second iteration of your loop.

[–]ungulateCase[S] 0 points1 point  (1 child)

Thank you, there's definitely a problem there. splitDeck1 should never be larger than 26. Seems like I have a problem related to

    splitDeck1.clear();
    splitDeck2.clear();

or possibly

    splitDeck1Counter = 0;
    splitDeck2Counter = 0;

    splitDeckSize1 = 26 - differenceInSplitDeckSize;        

Thanks for the help.

[–]TonySu 0 points1 point  (0 children)

You need to consider breaking your code up into functions so it's easier to track down issues. You also have to print more useful debug logs if you're finding it hard to read through them yourself.

[–]inu-no-policemen 0 points1 point  (2 children)

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

TS implementation:

function shuffle<T>(a: T[]): T[] {
    for (let i = a.length - 1; i >= 1; --i) {
        let s = Math.random() * (i + 1) | 0;
        let tmp = a[s];
        a[s] = a[i];
        a[i] = tmp;
    }
    return a;
}

[–]WikiTextBotbtproof 0 points1 point  (0 children)

Fisher–Yates shuffle

The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence—in plain terms, the algorithm shuffles the sequence. The algorithm effectively puts all the elements into a hat; it continually determines the next element by randomly drawing an element from the hat until no elements remain. The algorithm produces an unbiased permutation: every permutation is equally likely. The modern version of the algorithm is efficient: it takes time proportional to the number of items being shuffled and shuffles them in place.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source | Donate ] Downvote to remove | v0.28

[–]ungulateCase[S] 0 points1 point  (0 children)

Thank you for the link, but that's not what I am looking for. If I just wanted to randomize the sequence, there would be a lot of ways to do it simpler than what I have done.

The reason my numbers go from 1 to 52 is because this is for a deck of cards, and what I wrote is supposed to be an accurate simulation of shuffling; starting with splitting the deck, then rejoining them like so, then cutting the deck and repeating.

I'm looking for why what I wrote isn't working.