0

i want to try to put a unique random number from shuffled list to array but keep failing.

im using this answer with little modification.

this is my method

ArrayList<Integer> list = new ArrayList<>();
    for(int i=0;i<10;i++) {
        list.add(i);}
    Collections.shuffle(list);
    for(int x=0;x<3;x++){
        RNGee=RNG.nextInt(9);
        RN[x]=list.get(RNGee);
        if(RN[x]==QNum){
            x=0;
        }
    }

When i print out the RN array, some of it have a chance to get same value.

Is there something wrong with my code ? Please explain it to me. Thank you.

Community
  • 1
  • 1
Rafie Gilang
  • 31
  • 1
  • 9
  • if you want your implementation to be able to work then you need to remove the value from the list, and your `nextInt` call should use `list.size()` as parameter and not just 9. – SomeJavaGuy Oct 21 '15 at 10:47
  • 1
    Why are you doing this? The `list` is already shuffled - why do you need to get random indices from it? – Boris the Spider Oct 21 '15 at 10:48
  • By remove, Do you mean i have to remove the value from list that i put in the array ? – Rafie Gilang Oct 21 '15 at 10:49
  • @BoristheSpider i want to get a smaller chance of having same value in the array. Is it not an effective way to implement ? – Rafie Gilang Oct 21 '15 at 10:52
  • If the `list` consists of **unique** numbers, where would duplicates come from?? – Boris the Spider Oct 21 '15 at 10:52
  • @BoristheSpider well, im using the array as a parameter to set image button background but i often getting a same images in my buttons . i think the problem is from my array. – Rafie Gilang Oct 21 '15 at 10:57

2 Answers2

1

Your easiest way of doing this would just be to use list.get(0), list.get(1) and list.get(2). The list is shuffled, so the three elements at the head of the list will be random and different.

for(int x=0;x<3;x++){
    RN[x]=list.get(x);
    if(RN[x]==QNum) { //but this is very unclear
        x=0;
    }
}

There's part of your code that you haven't explained, though, which is the bit that sets the loop variable back to 0 if the random number you choose is equal to QNum. It's not at all clear what that's there for, but I suspect you will need to remove QNum from the list beforehand, so that it never turns up:

for(int i=0;i<10;i++) {
    if (i!=QNum) {
        list.add(i);
    }
}
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • Thank you so much for the help. Well i want to get array value that isnt the same with QNum and other value in that array. But it seems i messed it up.Once again thank you so much ^^. – Rafie Gilang Oct 21 '15 at 11:05
0

Despite the fact that your solution, as chiastic-security said, is unneccessary your mistake is that your list still contains 10 elements and that you are randomly picking from.

To be able to use your solution you would need to remove the element that you did select from the list and generate your random number in the range of 0-list.size-1.

ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++) {
    list.add(i);}
Collections.shuffle(list);
for(int x=0;x<3;x++){
    RNGee=RNG.nextInt(list.size()); // You collection gets smaller after each itteration so be sure to not run into a OutOfBound
    RN[x]=list.get(RNGee);
    list.remove(RNGee); // Remove the duplicate to be sure that the element doesn´t get picked twice
    if(RN[x]==QNum){
        x=0;
    }
}

Edit: also make sure to follow the java convention, which says that methods and variables should start lowercase and classes uppercase. That will make you code more readable to us and yourself in the end.

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33