简体   繁体   中英

Finding random numbers and a pattern from them

I have a large homework problem and none of my attempts to solve a small part of it work. The project is a game of life, so at each step the organism in a cell has to consider each of its surrounding cells. Diagonals aren't used and I want to start the consideration at a random cell.

I've approached it several ways but I honing in on this as the best.

Part one - find a random x and y that are a side, not a diagonal. I figured that to be a side abs(x) and abs(y) can't be the same. I loop through random tries until the abs()s are different.

Part two - use four if() to determine the current side and then change the x,y to the next side (going clockwise).

My results have been that the loop stops at odd places and never with abs()s different. I suspect the while() expression is wrong. I tried abs(x).equals(abs(y)) but found that method does not work on primitives. So I tried abs(x) != abs(y) but that leads to odd results.

Here is current effort. Any help would be appreciated.

   public static void main(String[] args)   {
      //jkbeg
       System.out.println("objective in this section is" +
            " to get a random side and then move clockwise around to others");
       int targetx; //these will hold the next cell to use.
       int targety;

//     Part One - get random side to be starting cell
//     n.b. abs(x) == abs(y) means diagnol cell, not a side
//     first get randoms of 0 or 1 into targets. 
//     keep trying randoms until abs() are not equal       
     do {   
         targetx = (int) (Math.round(Math.random()));
         targety = (int) (Math.round(Math.random()));
         System.out.println("rando, not checked yet"+targetx +","+targety);
         System.out.println("abs x "+Math.abs(targetx));
         System.out.println("abs y "+Math.abs(targetx));
     }while(Math.abs(targetx) == (Math.abs(targety))  );
     System.out.println("after check for sides, not corners"+targetx +","+targety);

//Part two - walk around the sides clockwise
         System.out.println("random start cell: "+targetx +","+targety);
     for(int targetCounter=0; targetCounter<4; targetCounter++){         
         if ((targetx==1)&&(targety==0)){targetx=0;targety=1;} //top to right
         if ((targetx==0)&&(targety==1)){targetx=-1;targety=0;} //right to bottom
         if ((targetx==-1)&&(targety==0)){targetx=0;targety=-1;} //bottom to left
         if ((targetx==0)&&(targety==-1)){targetx=1;targety=0;} //left to top
         System.out.println("next cell clockwise: "+targetx +","+targety);
     }
     System.out.println("end  *random side then walk around* here here");
     System.exit(0);
   } // main
      //jkend

My results have been that the loop stops at odd places and never with abs()s different. I suspect the while() expression is wrong

System.out.println("abs x "+Math.abs(targetx));
System.out.println("abs y "+Math.abs(targetx));

Check out the variable names in those lines.

Part II: Each of your if statements changes the value of targetx and targety such that the values match the next if statement. As a result, every if statement executes and the values of targetx and targety at the end of the loop are the values set in the last if statement.

You should be using an if-else if structure. Even if the values being set in each of the if statements did not match the next if statement, you would still want to use an if-else if structure because then java wouldn't have to check all 4 if conditions every time through the loop. With and if-else if structure, as soon as one if matches, the rest are skipped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM