Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly generating integer without repetition, no arrays

I'm writing a program that has 6 different pictures and displays 12 tiles, with two of each picture. It is a memory game, and I am working on generating a random tile placement. I need to use RandomIntGenerator to distribute the tiles throughout the board, but I can't think of a way to do this since RandomIntGenerator will duplicate numbers. I'm not allowed to use arrays, which would have made this much easier. Can you give me any insight into how this could be done?

Any help is really appreciated! -Ryan

like image 804
Ryan McClure Avatar asked Nov 25 '25 03:11

Ryan McClure


2 Answers

Create a List<Integer> with 12 values (1-12)

List<Integer> list = new ArrayList<Integer>(12);
for(int i = 0; i < 12 ; i++){ list.add(i);}

and then shuffle it

Collections.suffle(list)

like image 61
jmj Avatar answered Nov 26 '25 17:11

jmj


yes, it seems you need random shuffle of your cards. You could implement your version of simple Fisher-Yates shuffling, for example, or indeed, use Collections.shuffle()

like image 21
kiruwka Avatar answered Nov 26 '25 17:11

kiruwka