This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Random Numbers II

 
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
 
Wanderer
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...
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • 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...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE 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.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!