• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random Number Chooser programming exercise

 
Greenhorn
Posts: 7
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys
the task is:

Write a method that returns a random number between 1 and 54, excluding the numbers passed in the argument.
The method header is specified as follows:


The code I've wrote:


The end result for the random generated number should be a value different from the numbers in the array.
In this code I've managed to generate new random if the generated number is equal to one of the numbers in the array, but can't figure out how to test the new value again.
Could you point me the right direction to find my mistake?

Thanks a lot
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as I can see in your logic it has a bug. when


will check that the random no. generated is not equal to the current item in the array numbers[i] and onwards but what about the previous items ??. It doesn't consider the case in which it can be equal to the previous item in the list that is already checked.

for example take the case that you first generated the random no. as 22 in the for loop and then you generated the no. 10 in the while loop
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will suggest you to use list and use its contains() method to check if the no. generated should be excluded, if it says yes then generate new no. and check again else you have the no.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random numbers do not support any relationship between themselves. If you are constrained not to use the same number twice, that is not random numbers. I think it is called random selection from a shrinking population.
 
Venny Tank
Greenhorn
Posts: 7
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:as I can see in your logic it has a bug. when


will check that the random no. generated is not equal to the current item in the array numbers[i] and onwards but what about the previous items ??. It doesn't consider the case in which it can be equal to the previous item in the list that is already checked.

for example take the case that you first generated the random no. as 22 in the for loop and then you generated the no. 10 in the while loop



I'm still on single dimensional arrays chapter of the book that I'm learning from and can't use any more advanced techniques than that.
Is there any way I can check the random number against previous items?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you would have to keep a list of which ones you found, and each time you want a new one, make sure it isn't already in your list. If it is, you have to select again

Note that the more numbers you select, the slower it will become as you may have to re-try more and more often.

Alternatively, you could move them around...i.e. each time you pick a value, move it to the end of the array, and then next time select a value from 0 to (n-1)...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic