Bromon Original, please respect the copyright, the generation of playing cards
In the game, you need to generate a random playing card for the user. First we need to initialize a plate, put it in a HashMap, each card is represented by one byte, high as a representative color, representing the number, generates a settlement:
Package org.bromon.games.al;
Private static hashmap cards = new hashmap ();
INT TMP = 0;
For (int i = 0; i <4; i ) {
For (int m = 0; M <13; m ) {
TMP = ((byte) (i) << 4) | ((byte) m); // Use a bit operation to construct a card
Cards.put (New Integer (i * 13 m), New Byte (Byte) TMP);
}
}
Cards.Put (New Integer (53), New Byte (Byte) 0x4D); // Daw
Cards.Put (New Integer (54), New Byte (Byte) 0x4e)); // Xiao Wang
How to get the N cards in it? Our approach is to generate a random number of 0-55. Use this random number to get an object from HashMap, and then remove the object from the queue to avoid repeating. Since the random number in Java is generated according to time, it is possible to cause the user to get the card is not scattered. Every user is not a joke? So when we generate random numbers, we joined a large number of calculations:
Long cardid = new long (math.Round (math.rand ())). INTVALUE ();
By modifying this large number, you can control a user's card. Next: About thread