preventing same random numbers in JAVA

This is a discussion on preventing same random numbers in JAVA within the Tech Board forums, part of the Community Boards category; This is actually something I need help with in a java assignment. But I am pretty sure the concept is ...

preventing same random numbers in JAVA

This is actually something I need help with in a java assignment. But I am pretty sure the concept is similar to C++. In other words if you know C++ you can probably help me with this minor set back.

I basically want to generate random numbers (from 0 to 9). However I do not want it to generate duplicate random numbers. I have been messing around with this for a few hours but still can not come up with something to prevent the recurring of the same numbers. for example I do not want this......8, 8, 3, 5, 6. Where 8 is duplicated. I do not want that. Below is a snippet of part of my assignment that I have been messing around with trying to prevent this problem. So far I am stumped.

Uhh I am not too familiar with that much c++ or at least all the STL Standard Templated Libraries.

This is java but I feel I can get help for this particular problem I am having despite it being a C++ forum.

Basically I am filling an array with random values from 0-9. But I do not want to end up with duplicate values in. For example I do not want to end up with duplicate 0's, 1's, 7's, etc. How would I go about to prevent that?

Oh, I think I have come up with an idea that might work on Java. If there are variable-sized containers in Java you can have a container hold {0, 1, 2 ... 9} and another empty container. Generate a random number which will be between 0 and "length" and then use that number as the index of the first container.

From the original post it is unclear whether you want each random number to simply be different from just the one preceeding it, or from all that have preceeded it.

For the former, jsut remember the last value and next time, loop until the result is different.

For the later, you have two choices, keep a set of values that have not been used already, or keep a set of values that have been used already, then make sure it is or isn't (respectively) in the set. Set can entail a list, array, binary tree, hash table, or other container.

Using random_shuffle is the best way to go, and it doesn't take much knowledge of the STL (you can even use it on a C style array).

If you cannot use random_shuffle, then just use the algorithm it uses. That algorithm is small but easy to get slightly off.

Searching for random_shuffle on this site should give an example of its implementation, I know I've posted in a few threads that have done this.

>> jsut remember the last value and next time, loop until the result is different.
I don't like this solution in general because it is needlessly inefficient. For 10 items it is less of a big deal, but still the random_shuffle algorithm isn't that hard.

Uhh I am not too familiar with that much c++ or at least all the STL Standard Templated Libraries.

This is java but I feel I can get help for this particular problem I am having despite it being a C++ forum.

Basically I am filling an array with random values from 0-9. But I do not want to end up with duplicate values in. For example I do not want to end up with duplicate 0's, 1's, 7's, etc. How would I go about to prevent that?

Thank you

1. Check after you generated a number if it's already in the array, if yes, generate a new one.
2. If you generate from 0-9 only, make sure your array is not bigger than 10, or else you got yourself an infinite loop.

This is of course a horrible solution to the problem, because with an array of size 10, the chance for the last number to be different from the other numbers is 1/10 and your program might just loop unnecessarily before finding the last number.

This is of course a horrible solution to the problem, because with an array of size 10, the chance for the last number to be different from the other numbers is 1/10 and your program might just loop unnecessarily before finding the last number.

Well, then, I'd say a better approach would be to start by creating a 10 element array and populating it with numbers 0-9. Then you can iterate through the array, choose a random number, say n, and swap the current element with the nth element. This way you get a random assorted array, no duplicates, and you don't redundantly check for used numbers. Plus it's incredibly simple to implement.