• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Array random numbers no duplicates

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,
I am looking for help regarding an assignment. I need to to make an array of 5 "random" integers but no duplicates are allowed. The elements of the array should be from 0 to 9. I have to use reduced number of `if` statements and I need to use the knowledge that I already have. So I have to use Math.random(). I haven`t learned about collection.shuffle, or arrayList,
I have created a code after hours of struggling, but it still does have duplicates. I have run out of ideas. Would it be possible to fix my code without changing it significantly?



Thank you in advance
 
Sheriff
Posts: 15996
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randomness does not imply uniqueness. What you can do is to create the set of unique numbers first then randomize their order. That means it's a two step process. First, add all the numbers you want. Second, shuffle the numbers randomly. There is a standard shuffle() method on the java.util.Collections class.  https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html

 
Saloon Keeper
Posts: 22803
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A truly random set of numbers could have the name number repeat many times. Similar to flipping 100 pennies and getting 99 heads and one tail. Probability theory is, in fact, concerned with calculating the odds on such an event (whuch incidentally is something like ½*½**½... for a grand total of 100 terms. And there's a much more compact mathematical notation for that, but it's hard enough to display fractions here.

However, the case where a number must not repeat is a common one. For example, you cannot draw more than one Ace of Spades from an honest standard playing-card deck.

I don't think there's anything in the standard Java repertoire to implement that directly, so probably the best you can do is set up a Map, save each drawn number as a key, then when you pull the next random number see if it's already in the map, and if so, keep pulling until you get a value that isn't.
 
Junilu Lacar
Sheriff
Posts: 15996
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I see you have some constraints in what you can use. Idea is the same though. You create your array with the elements in order. That should be easily done with a single for-loop. Next, you'd shuffle the elements in the array. Since you can't use Collections.shuffle, you'll have to do this yourself. I assume you know how to swap elements in an array using a temporary variable. To shuffle your array, you'll need to iterate over it several times. Each pass through, you'll swap the current element with a randomly chosen element. To fully randomize a deck of cards, you have to shuffle it at least 7 times so that might be a good number of times to iterate over your array as well. After you have shuffled the array this way, you just pick the first N elements of the array to get your randomly ordered unique numbers.
 
Junilu Lacar
Sheriff
Posts: 15996
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In high-level code, you'd do something like this:

So the exercise really boils down to implementing the subtasks of shuffle() and displayFirst().
 
Gabriel Sabo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the insight and the time you put into answering my question. Take care
 
Master Rancher
Posts: 3723
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gabrel Sabo wrote:I need to to make an array of 5 "random" integers but no duplicates are allowed.


I think when a user puts "random" in quotes like this, it's indicating they already know that this situation isn't truly random.  We can understand it as "as random as possible subject to the following constraint".  We don't need to re-explain that it isn't really random.

Gabrel, while I think the others are trying to guide you to the best solution(s), I think there's also a lot of value in looking at the code you've actually written so far.  It's not too far off from solving your problem.  But there are problems in this area:

Gabrel Sabo wrote:


The issues to think about here are: is this really detecting duplicates?  And, if you do find a duplicate, what should you do about it?  I suggest you should study this more carefully to see what the code is really doing right now.  I suggest you can add some print statements to see what is happening as you go through the loop.  When does it find a "duplicate"?  When *should* it find a duplicate?  Printing info as you go is a good way to follow what your program is doing.
 
Mike Simmons
Master Rancher
Posts: 3723
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, I don't know why there's a > on line two of both my code blocks in the last post - I didn't put one there, and no > is visible when I edit the post.  Perhaps there's a bug in the forums software?
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Incidentally, I don't know why there's a > . . . .  Perhaps there's a bug in the forums software?

I think there is, yes. It is seasonal, like real‑life bugs. It disappears for months and then reappears to annoy us.

I think the current problem is supposed to b called random sampling from a diminishing population (or similar). It is possible to create a Stream to solve it, but it is a bit inefficient because it may necessitate deletion of many duplicate values. Also its memory requirement is proportional to the size of the output List.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic