android - Character assignment from a shuffled array -
(edited, more clear) i'm learning code android , make program randomly select single number range, run random select of remaining numbers specified iterations.
4 iterations in example:
- 1 2 3 4 5 6 7 8 9 10, randomly select integer set, "7", new set,
- 1 2 3 4 5 6 8 9 10, randomized, select integer new set, "2", new set,
- 1 3 4 5 6 8 9 10, randomly select integer new set, "9", new set,
- 1 3 4 5 6 8 10, randomly select integer, "6".
output 2, 6, 7, 9.
would using character assignment, a=1, b=2 ect, shuffled array? want first random selected number not present in consecutive sets randomized. where, finally, 7,2 , 9, in example, not in last set. focus not on avoiding duplication generating new specific non-sequential set randomly choose unique number from.
take @ following example:
public static void main(string args[]) { // create our test case list<integer> integerlist = new arraylist<integer>(); for(int i=0; i<10; i++) { integerlist.add(i+1); } // print out 4 randomly chosen elements list random rand = new random(); for(int i=0; i<4; i++) { system.out.println(selectone(rand, integerlist)); } } private static integer selectone(random rand, list<integer> integerlist) { int randomindex = rand.nextint(integerlist.size()); return integerlist.remove(randomindex); }
to each successive call selectone, random element removed , returned. since cannot remove same item twice, each element in list, selected once. works series of numbers deck of cards.
in fact, if use following, use list<integer>
list<card>
:
private static <t> t selectone(random rand, list<t> integerlist) { int randomindex = rand.nextint(integerlist.size()); return integerlist.remove(randomindex); }
Comments
Post a Comment