I'm making a memory game in obj-c. I have 6 UIButtons as cards and 3 pairs of pictures. I just arc4Random() % 3
for each button to get a new image every time view loads. My problem is that, obviously, for example all buttons could have the same image since it just takes a random number. How can I make sure only 2 images of each pair is generated?
Asked
Active
Viewed 676 times
0

Wilhelm Michaelsen
- 665
- 14
- 32
-
This doesn't answer your question but if I was you I would look at the Stanford iTunesU course. They actually implement this exact game. It is also extremely informational beyond just the game implementation. – Firo May 15 '13 at 23:20
-
1To avoid modulo bias `arc4random_uniform(3)` should be used instead of `arc4Random() % 3`, see man page. – Gerd K May 15 '13 at 23:54
-
@Firo Thanks I'm going to take a look at it! – Wilhelm Michaelsen May 16 '13 at 06:57
-
@GerdK what is the difference? – Wilhelm Michaelsen May 16 '13 at 06:57
-
@Wilhelm Michaelsen See [this discussion about modulo bias](http://stackoverflow.com/questions/648739/objective-c-modulo-bias). – Gerd K May 16 '13 at 13:54
-
@Firo where can I find that tutorial? – Wilhelm Michaelsen May 18 '13 at 11:35
-
Here's a link to the courses, you can also find then in iTunes (in iTunes U section, I download them so I can watch them offline): https://itunes.apple.com/us/course/coding-together-developing/id593208016 --- Here is a link to the course material: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/ – Firo May 20 '13 at 17:52
1 Answers
1
You can add the images in an array, shuffle it, and then iterate on it, setting the buttons' images.

Marcelo
- 9,916
- 3
- 43
- 52