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?
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.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
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.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
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.
posted 1 month ago
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?
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: