Did you work out on paper what you are going to do before you wrote that code? What you appear to have is a variation of the Collections#shuffle(List) method. That doesn't riffle anything. What you want is the cards in this order if you have 0‑based indices:-That new pack of cards retains all the information from the original, which allows you to un‑shuffle your cards easily.
When you do your algorithms module, you will find out that those shuffling algorithms have pecularities, which are discussed here and three of the four links in this post. I don't like arithmetic with Math#random, which is error‑prone. The other link from that post discusses what I prefer to use instead.
I am liable to go into hiding for the rest of the day.
so I would ask...what is your real goal here? Are you trying to re-arrange the cards in the specific order as Campbell listed, or are you trying to randomly re-arrange them, like a riffle-shuffle does? in the real world, it is almost impossible to do a perfect riffle, which is why the cards end up randomized.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Here's the JUnit 5 test that I used to TDD a solution for this:
The only question this test would bring to attention is whether or not an array with only two elements should remain unchanged. If you want [1, 2] to be riffle shuffled as [2, 1] then that would make it necessary to either complicate the logic a little bit or, for consistency sake, change the specification that the riffled array would start with the first element of the first half.
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems: