Results 1 to 5 of 5
  1. #1
    debugdoug is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default choosing random array to print to screen

    This is most likely relatively simple. And there is probably a better way to do what I am doing. I have 2 arrays that I have cast random numbers into. I only want to display the results from 1 of the arrays randomly selected. Eventually I will have more arrays at variable lengths, but doing it with 2 will show me how to use it in a larger scope.

    Java Code:
    import java.util.Random;
    
    public class test{
    
    	public static void main(String[] args){
    
    		//”test and test2” are pointers.
    		int[] test = {1, 2, 3, 4};
    		int[] test2 = {1,2,3};
    		Random rand = new Random();
    	    
    	    for (int x=0;x<test.length;x++){
    	    	test[x] = -1000 + rand.nextInt(999 - -1000 + 1); 
    	    	System.out.println(test[x]);
    	    }
    
    	    for (int x=0;x<test2.length;x++){
    	    	test2[x] = -1000 + rand.nextInt(999 - -1000 + 1); 
    	    	System.out.println(test2[x]);
    	    }
    	}
    }
    I would just like to know how to randomly select 1 of the arrays and print it.

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can do a random number and if it's greater than x, print array 1, if it's less than, print array y. A better way would probably to store the arrays in an array and randomly index into the first part of the array.

    Java Code:
    int[][] array = new int[2][5];
    //array initializing
    print array[random];

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    Well, you could say

    Java Code:
    // at end of main()
    int[] toDisplay;
    if(/*some 'randomly' true condition*/) {
        toDisplay = test;
    } else {
        toDisplay = test2;
    }
    // now display the array toDisplay...
    The boolean condition is obtained from a random process: like rand.nextInt(2)<1. (a "coin").

    Notice that nothing is copied here. The value of test (or test2) is a reference to the array and this reference is assigned to a new variable. SO, from that point of view, it is straightforward.

    The problem - of course! - is that it won't scale as you would like.

    So... Arrange things so that your arrays are, themselves, elements of an array! Then choose the index of this outer array at random. (If you haven't encountered them, find out about 2 - or multi - dimensional arrays.)

  4. #4
    debugdoug is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I must admit that I am having trouble getting the arrays stored as array values themselves. Can someone get me pointed in the right direction. I have been researching multidimension arrays but cannot figure how to cast the array into an array.

  5. #5
    debugdoug is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I got this figured out. Here is how I did it.

    I did this for each row of my multidimmensional array.
    Java Code:
    for (int x=0;x<myArray[0].length;x++){
    	    	myArray[0][x] = -1000 + rand.nextInt(999 - -1000 + 1); 
    	    }
    Thanks.

Similar Threads

  1. Getting random word to print out in an Array
    By manifest3r in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 11:46 PM
  2. Replies: 3
    Last Post: 08-21-2010, 10:01 AM
  3. Choosing which paper tray/feeder to print to
    By r00tb33r in forum AWT / Swing
    Replies: 0
    Last Post: 08-01-2010, 08:10 AM
  4. print on the client screen
    By a_maged in forum Networking
    Replies: 0
    Last Post: 12-17-2007, 05:10 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •