14. marts 2005 - 14:06
Der er
4 kommentarer og 1 løsning
Shuffling cards
I have a Vector with 52 objects of type "Card" (which consist of an int (value) and a String (color)). I need some kind of method to "shuffle" the cards, or randomize the order of them. As of now, I use: Collections.sort(_cards, _shuffler); where _shuffler is a Comparator looking like this: public int compare(Object o1, Object o2) { return (int)((Math.random() - 0.5) * 100); } Can you see any drawbacks with my shuffling method? Can you think of a better one. More accurate, or perhaps faster? Answer in danish, swedish or english :)
Annonceindlæg fra Novataris
Vejen til devops med Bavarian Nordic
Bavarian Nordics vækst blev starten på et DevOps-samarbejde med Novataris for hurtigt at kunne tilpasse IT-organisation til forretningen.
7. december 2023
14. marts 2005 - 16:07
#4
arne_v >> hehe, maybe I should have read more. Just what I am looking for, and way faster than my own algorhitm. To be sure that if was shuffled, I ran my "Collections.sort(...)" ten times. Do you think I need to do that with "Collections.shuffle()" ? And feel free to place an answer :) Also thanks to fgsupermand for telling me about nextInt(), although I will not use this implementation more