• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Numbers II

 
Andrew Wood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obviously you would compare the arrays in a loop to figure this out. given the arrays are the same size.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic