• 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

"powerball" loop help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I've recently started on a java assignment, and I'm having a few troubles writing the code. The goal of the program is to basically generate 5 random numbers between 1-32 without any repeats. I can generate the 5 numbers no problem, but I'm not quite sure how to keep numbers from repeating. Here's what I have so far:

Does anyone have any suggestions? I know my method is probably really inefficient, but I'll appreciate any help I can get.
Thanks
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you allowed to use an ArrayList? Or an array?
 
Joseph Ivanchuk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Are you allowed to use an ArrayList? Or an array?



I believe so, I'm not too familiar arrays though. I'm fairly new to java.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randomness and uniqueness are not the same. If you randomly generate 50 numbers that are in the range [1..5] you will almost certainly get some repetitions.

What you need to do is shuffle the list. That is, you iterate through your list, swapping random elements.  The standard library already has such a utility: https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#shuffle-java.util.List- but it's easy enough to implement this yourself; in fact, it's a good exercise to try to do it.  

Note that you need seven shuffles to completely randomize a deck of cards. I don't know how many it would take for the list of possible Powerball numbers.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, we'll use an array.

Say, we have an array with 32 element, with indeces 0-31. We will use this array to keep an eye on the numbers already drawn.
Now, if we draw a random number, say 10, then we check if array[10] == 0. If not, then that indicates that 10 is already drawn. If so, we keep trying a new random number i until array[i] == 0.
If that succeeds, we put a 1 in array[i], so that we know it has been drawn.
When we have five (or whatever other number you like) filled elements in our array, then we print out these elements and we're done.

There are far more elegant ways, but this one is simple enough.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Junilu

Joseph Ivanchuk wrote:I believe so, I'm not too familiar arrays though. I'm fairly new to java.


Therefore I did not want to use Lists and its methods.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:
Now, if we draw a random number, say 10, then we check if array[10] == 0. If not, then that indicates that 10 is already drawn. If so, we keep trying a new random number i until array[i] == 0.
If that succeeds, we put a 1 in array[i], so that we know it has been drawn.


Why not just use a boolean array then?

There are far more elegant ways, but this one is simple enough.


If you use a boolean array, this solution is not bad.

If you don't want to use a boolean array, you can just move a number that has been "drawn" to the "end" of the array, where the "end" is always moved down towards 0 after a number is drawn and swapped. You'd also adjust the range from which you pick the next number to draw.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It hardly matters if you use a boolean or not, I/m not sure why you make that such an important issue. But I would like to hear from OP, what he thinks of the suggestions so far.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Boolean fits better with the idea you're attaching to 1 or 0 to indicate drawn or not drawn. I'm not sure why you don't think that's an important distinction.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method nextInt(int bound) returns value between 0 (inclusive) and the specified value (exclusive). If you want generated numbers inclusive of 32 then you need specified value 32 because nextInt(31) can return 30 as max number so 30 + 1 = 31.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic