File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Random Numbers II Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Random Numbers II" Watch "Random Numbers II" New topic

Random Numbers II

Andrew Wood

Joined: Jul 25, 2005
Posts: 4
OK well i'd just like to say thanks for everybody that replied Random Numbers! But i have one further question...

Lets say I have an array (Array1) of number in any sequence of 1,2,3,4,5,6,7,8,9. Lets say I then have another array (Array2) of numbers between 1 and 9.

How do i make it so that, say, Array1[2] and Array2[2] are not the same i.e. a following situation is avoided?

1,2, 3 ,4,5,6,7,8,9,
9,8, 3 ,7,6,5,4,3,2

Thanks v. much
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Do you already have the two arrays, or are you trying to generate the second array? In the first case, you can just test each element. In the second case - well that's a bit more complex. One possibility is to simply generate a second array as you did in your previous problem, then test if there are any duplicates. If there are, simply try again. Repeat until you have a valid second array. This probably isn't very efficient, but it's simple based on what you've already done. Another thing to consider: do you just need to generate a second array, or might you also need to gernate a third, fourth, fifth etc. up to ninth? In this last case you will probably have to be a bit more clever in finding an algorithm...

"I'm not back." - Bill Harding, Twister
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Just shift the second array for one element:
1, 2, 3, 4, 5, 6, 7, 8, 9
2, 3, 4, 5, 6, 7, 8, 9, 1

Some people would prefer to shift in the other direction:
1, 2, 3, 4, 5, 6, 7, 8, 9
9, 1, 2, 3, 4, 5, 6, 7, 8

but I would clearly prefer the first solution.
Andrew Wood

Joined: Jul 25, 2005
Posts: 4
Thanks Stefan. I get the idea, but what I forgot to mention was that each array had to have the order of numbers randomly generated e.g. 1, 2, 6, 4, 3, 5, 9, 8, 7...
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

obviously you would compare the arrays in a loop to figure this out. given the arrays are the same size.
I agree. Here's the link:
subject: Random Numbers II
It's not a secret anymore!