java - Swapping integer array values in a for loop - Is it possible? -


so decided create simple card deck shuffling program, nothing fancy single array 52 "cards" numbered 0 52. i'm not sure if best method.

i created random integer method (randint) random numbers , shuffling method (shuffle) gets 2 random cards , swaps values.

my method simple enough when run code (and debug code added) seem each value becoming exact same value (d[2] = d[1] = randcard1) results in multiples of same card , other values disappearing. given enough shuffles whole deck become 52 cards consisting of same value.

i have read questions swapping integer arrays question different issue of using loop, (as method of swapping array values similar questions already, final/static keywords aren't helpful when modifying few times have concluded loops blame) apparently prevents arrays updating properly.

is case if how on come , if not missing? there problem way of swapping integer array values?

here code:

import java.util.random;  public class cards {      public static void main(string[] args) {          int[] deck = new int[] {             0, 1, 2, 3, 4, 5, 6, 7, 8, 9,             10, 11, 12, 13, 14, 15, 16, 17, 18, 19,             20, 21, 22, 23, 24, 25, 26, 27, 28, 29,             30, 31, 32, 33, 34, 35, 36, 37, 38, 39,             40, 41, 42, 43, 44, 45, 46, 47, 48, 49,             50, 51         };          printarray(deck, "original deck:");         deck = shuffle(deck);         printarray(deck, "shuffled deck:");         }          public static int[] shuffle(int[] d) {             //shuffle between 100 200 ensure thorough shuffle             int randloops = randint(100, 200);             int randcard1;             int randcard2;          (int = 0; <= randloops; i++) {             int temp;              randcard1 = randint(0, 51);              {randcard2 = randint(0, 51);}             while (randcard2 == randcard1);              system.out.print("*" + randcard1 + "|" + randcard2 + "/");             temp = randcard1;             d[randcard1] = d[randcard2];             d[randcard2] = d[temp];             system.out.println(d[randcard1] + "|" + d[randcard2] + "*");         }         return d;     }      public static int randint(int min, int max) {         random rand = new random();             int randomnum = rand.nextint((max - min) + 1) + min;          return randomnum;     }      public static void printarray(int[] d, string t) {         system.out.println(t);         (int = 0; < d.length; i++) {                 system.out.print(d[i] + ", ");         }          system.out.println();     } } 

here example of output debugging code (hopefully helps , doesn't hinder):

original deck: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,  *40|41/41|41* *29|40/41|41* *19|26/26|26* *2|27/27|27* *4|30/30|30* *24|25/25|25* *7|1/1|1* *17|47/47|47* *14|5/5|5* *42|11/11|11* *23|42/11|11* *33|30/30|30* *19|42/9|9* *11|26/9|9* *40|21/9|9* *36|11/9|9*     <--------- @ end don't know why values = 9? shuffled deck: 9, 11, 30, 11, 30, 30, 31, 45, 11, 11, 51, 9, 11, 45, 45, 45, 51, 11, 9, 9, 44, 9, 30, 44, 27, 11, 9, 27, 31, 11, 11, 21, 11, 11, 45, 35, 9, 11, 11, 30, 9, 11, 9, 11, 21, 31, 9, 30, 45, 30, 11, 45,  build successful (total time: 2 seconds) 

this problem lies:

temp = randcard1; d[randcard1] = d[randcard2]; d[randcard2] = d[temp]; 

instead of saving card value in temp, you're saving index. need instead

temp = d[randcard1]; d[randcard1] = d[randcard2]; d[randcard2] = temp; 

note jdk has shuffle method:

// create deck integer[] deck = new integer[52]; (int = 0; < deck.length; i++) {     deck[i] = i; }  // shuffle collections.shuffle(arrays.aslist(deck)); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -