Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Creating a riffle shuffle program on java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, so i have been trying to create a program that performs a riffle shuffle, where a deck of cards is split in two then interleaved into each other so like:
1,2,3,4
5,6,7,8
result:
1,5,2,6,3,7,4,8

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you just copy/paste it into your post?
Remember to use code tags (there's a code button at the top).
Ensure the code is between the open and close tags.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matty Stone wrote:where a deck of cards is split in two then interleaved into each other so like:
1,2,3,4
5,6,7,8
result:
1,5,2,6,3,7,4,8


The intent as stated above seems pretty unambiguous to me. It's also true, however, that the given code does not implement this stated intent.
 
Junilu Lacar
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me this can be accomplished with one pass of a for-loop.
 
Junilu Lacar
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic