• Post Reply Bookmark Topic Watch Topic
  • New Topic

Shuffle a deck of tarot card  RSS feed

 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends ,
here is some code to shuffle string array ,



objective is to swap each element of the array with a random element , so will my code work , is it good for demonstration

thank you
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you run through that loop several times.
No, I am afraid it is not particularly good. Your indentation which I corrected when I added the code tags was poor with far too much space between lines. Look here and you will find suggestions for making indentation etc much easier.
you should also be consistent with spaces around operators.
I would prefer not to use Math#random() for that sort of thing; see your other thread.
You will find you are swapping elements in arrays frequently. In whih case, you should have an ArrayUtils utility class with a static method like thisThen you can call it from inside your other methods like this ArrayUtils.swapElements(myArray, index1, index2);
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you would have a method in ArrayUtils which shuffles an array.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why reinvent the wheel?

Collections.shuffle();
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Why reinvent the wheel?

Collections.shuffle();


The only good reason I can think of is as a training exercise.

I been wondering if CS graduates these days have ever implemented a linked list, b-tree, hash table or bubble sort, merge sort or any of the basic algorithms just to really understand whats going on in the extensive libraries we have available. What's in a 21st century Data Structures and Algorithms class?

I completely agree it's counterproductive to roll your own when thoroughly tested, supported libraries are publicly available for production work.

Joe
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, linked lists, binary trees (also red-black trees, heaps and tries), hash tables, merge-sort, heap-sort, insertion-sort, selection-sort, quick-sort, multi-way/external sorting, binary search and stuff like graph traversal are all treated in D&A classes in universities and colleges, at least in the Netherlands.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yes, linked lists, binary trees (also red-black trees, heaps and tries), hash tables, merge-sort, heap-sort, insertion-sort, selection-sort, quick-sort, multi-way/external sorting, binary search and stuff like graph traversal are all treated in D&A classes in universities and colleges, at least in the Netherlands.

Thanks Stephan,

Last D&A class I had was in 1970 maybe 71.

Joe
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Why reinvent the wheel?

Collections.shuffle();

Do they, in nowadays D&A classes, teach that OP's array is not a List?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

What they should teach is not to use arrays when you have lists. If you *really* need to use an array to model a deck of cards (which I find very questionable), I would still prefer to shuffle it like:

Collections.shuffle(Arrays.asList(deck));
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yes, linked lists, binary trees (also red-black trees, heaps and tries), hash tables, merge-sort, heap-sort, insertion-sort, selection-sort, quick-sort, multi-way/external sorting, binary search and stuff like graph traversal are all treated in D&A classes in universities and colleges, at least in the Netherlands.


I did the Coursera Algorithms MOOCs (run by Princeton), which should be fairly representative of what at least one University covers nowadays, and what you've written there reads pretty much exactly like the syllabus of those.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yes.

What they should teach is not to use arrays when you have lists. If you *really* need to use an array to model a deck of cards (which I find very questionable), I would still prefer to shuffle it like:

Collections.shuffle(Arrays.asList(deck));

Whilst I totally agree, this is however not what OP's question is about. Although there are better ways to shuffle
an array, OP's algorithm is okay, and if his program runs, it is fine for demonstration.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Areeda wrote:
Stephan van Hulst wrote:Why reinvent the wheel?

Collections.shuffle();


The only good reason I can think of is as a training exercise.

I been wondering if CS graduates these days have ever implemented a linked list, b-tree, hash table or bubble sort, merge sort or any of the basic algorithms just to really understand whats going on in the extensive libraries we have available. What's in a 21st century Data Structures and Algorithms class?

I completely agree it's counterproductive to roll your own when thoroughly tested, supported libraries are publicly available for production work.

Joe

hello
I know this is not a professional level code , but actually I am explaining a tarot card reading program to my friends (console app) that i created myself . I want to make things very elaborate so that they can easily understand it . I run my code many times and it was doing what it should do but still I want to listen what others have to say .
thank you for your reply
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
I know this is not a professional level code , but actually I am explaining a tarot card reading program to my friends (console app) that i created myself . I want to make things very elaborate so that they can easily understand it . I run my code many times and it was doing what it should do but still I want to listen what others have to say
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!