Results 1 to 5 of 5
- 04-01-2011, 08:56 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
I would just like to know how to randomly select 1 of the arrays and print it.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]); } } }
Thank you.
- 04-01-2011, 10:10 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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];
- 04-01-2011, 10:13 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Well, you could say
The boolean condition is obtained from a random process: like rand.nextInt(2)<1. (a "coin").Java Code:// at end of main() int[] toDisplay; if(/*some 'randomly' true condition*/) { toDisplay = test; } else { toDisplay = test2; } // now display the array toDisplay...
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.)
- 04-05-2011, 06:34 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-05-2011, 03:45 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Getting random word to print out in an Array
By manifest3r in forum New To JavaReplies: 2Last Post: 02-16-2011, 10:46 PM -
Choosing which paper tray/feeder to print to (lets try this again)
By r00tb33r in forum AWT / SwingReplies: 3Last Post: 08-21-2010, 09:01 AM -
Choosing which paper tray/feeder to print to
By r00tb33r in forum AWT / SwingReplies: 0Last Post: 08-01-2010, 07:10 AM -
print on the client screen
By a_maged in forum NetworkingReplies: 0Last Post: 12-17-2007, 04:10 PM -
How do i tell it to print out the result to the screen???
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks