CaptainPinko
08-22-2005, 04:49 PM
I am building a game and need to shuffle a deck of cards and am trying to be creative on my own (it's more of challenge and better learning experience to come up with a good solution than to read the perfect solution). So I came up with this algorithim/approach. I'm looking for criticism.
1) Create a wrapper for your objects that are to be 'shuffled' that consist of an 'index' and a pointer.
class ShuffleSlot {
private int index;
private Object obj;
ShuffleSlot (Object obj, int idex){
this.obj = obj;
this.index = index;
}
int getIndex () {return this.index;}
}
2) Now as you iterate through your collection of elements drop each one into a wrapper and assign it a random number from lets say 0 to 10 or 100 times your collection size and add the wrapper into a binary tree* sorted by their random index number. (I mentioned making the index range larger than the sample to prevent collisions since most binary-trees are made as in-order sorts so having collisions would preserve some of the order.)
3) Do a traversal on your tree (shouldn't matter what kind) and there you have a random ordering.
4) If in practice this is not random enough then you could repeat the process a certain number of times proportional to the sample size. (say 1/5).
Benefits:
+ even if you don't know size of the sample it won't break (like overflow).
+ most languages provide binary-trees for you as part of standard libraries.
+ order on insertion is log n so it seems like it would be effecient
Criticisms:
- floating point is slow
- binary trees eat up memory
*not sure if an AVL tree is a benefit here... it will have faster insertions but since we aren't searching thats no help against the overhead and i don't think the shape of the tree affects the traversal time.
1) Create a wrapper for your objects that are to be 'shuffled' that consist of an 'index' and a pointer.
class ShuffleSlot {
private int index;
private Object obj;
ShuffleSlot (Object obj, int idex){
this.obj = obj;
this.index = index;
}
int getIndex () {return this.index;}
}
2) Now as you iterate through your collection of elements drop each one into a wrapper and assign it a random number from lets say 0 to 10 or 100 times your collection size and add the wrapper into a binary tree* sorted by their random index number. (I mentioned making the index range larger than the sample to prevent collisions since most binary-trees are made as in-order sorts so having collisions would preserve some of the order.)
3) Do a traversal on your tree (shouldn't matter what kind) and there you have a random ordering.
4) If in practice this is not random enough then you could repeat the process a certain number of times proportional to the sample size. (say 1/5).
Benefits:
+ even if you don't know size of the sample it won't break (like overflow).
+ most languages provide binary-trees for you as part of standard libraries.
+ order on insertion is log n so it seems like it would be effecient
Criticisms:
- floating point is slow
- binary trees eat up memory
*not sure if an AVL tree is a benefit here... it will have faster insertions but since we aren't searching thats no help against the overhead and i don't think the shape of the tree affects the traversal time.