Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Random Generator and 2D Arrays

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Generator and 2D Arrays

    So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there
    I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot
    making it look like there are only four 1s. How would i go about fixing that?



     
    package Grade12;
     
    import java.util.Random;
     
    public class Ghost {
     
    	public static void main(String[] args) {
     
    		Random generator = new Random();
     
    		int gameboard [][] = new int [8][8];
    		int randomx, randomy, counter = 0, sum = 0;
     
    		for(int row = 0; row < 8; row++){
    			for(int col = 0; col < 8; col++){
     
     
    			(gameboard[row][col]) = 0;
     
    			}
     
    		}
     
    		for(int row = 0; row < 8; row++){
    			for(int col = 0; col < 8; col++){
     
     
    				if( counter != 5){
     
     
    				randomx = (int)(Math.random()*8);
    				randomy = (int)(Math.random()*8);
     
    				gameboard[randomx][randomy] = 1;
     
     
    				counter++;
     
    				if(gameboard[randomx][randomy] == 1){
    					sum++;
    				}
     
     
     
    				}
    				System.out.print(gameboard[row][col] + " ");
     
     
     
     
    			}
     
     
     
     
     
    		}
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    	}
     
    }
    Last edited by hiya54; September 21st, 2014 at 09:21 PM.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Random Generator and 2D Arrays

    You could check to see if a 1 is in that "slot" and if there is already a 1 there, then you should redo the random number generator until it finds an open slot.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Generator and 2D Arrays

    So do i need a if loop in the nested for loop? I know i had to do something like that i just didnt figure
    how to do it yet

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Random Generator and 2D Arrays

    if(gameboard[randomx][randomy] == 0)
    {
    gameboard[randomx][randomy] = 1;
    counter++;
    }
    That should be under your randomx and randomy math generators.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Random Generator and 2D Arrays

    What camel-man describes could work in practice, but in theory it might end in an infinite loop. Most of the time that isnt too bad because its very very very unlikely, but I would personally not like to use a possibly unsafe method.
    As an alternative solution that always terminates you could use a list. You fill it with ALL possible positions, then you shuffle it, and then you take the first 5 elements in the list. Its guaranteed to give you 5 different random positions if you have at least 5 positions. If there are no 5 positions (because your array is too small) an appropriate exception will be thrown instead of an infinite loop.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Generator and 2D Arrays

    We werent taught array list or shuffle yet. I learned a bit of array list on my own spare time
    but yea not really that well, but anyways camel-man's method works with a simple while loop

Similar Threads

  1. Random Shape Generator
    By sjdganc in forum What's Wrong With My Code?
    Replies: 29
    Last Post: August 31st, 2014, 07:33 PM
  2. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  3. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  4. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 11:00 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM