iOS Tutorials: Rolling Dice Apps


We will be making an app that will role the dices for us. Leave your dices at home and bring your smartphone with you out. The idea here is when we click on the button, it will roll two dices randomly.

Create your project and access your Storyboard and pull out two image view and one button.

Download the images of dices here and copy them onto your Assets.xcassets

Create the IBOutlet for the images and IBAction for the button.

class ViewController: UIViewController {
    @IBOutlet weak var dice1: UIImageView!
    @IBOutlet weak var dice2: UIImageView!
    @IBAction func rollButtonPressed(_ sender: UIButton) {

We will create an array of dices images according to the names of the images.

let diceImages = ["dice1", "dice2","dice3","dice4","dice5","dice6"]

Since there are 6 informations in the array, we need to keep track of the images and we can do that by creating a counter.

var index1: Int = 0
var index2: Int = 0

Now, we need a function that could randomly show the images. I’ve created a new function for that purpose. arc4random_uniform is a function created by Apple to randomize number and it takes one parameter. By putting 6 in the parameter, it will return a number in between 0 – 5. And when it returns a random number, the number will reflect the position on the array and return the exact dice. 

func randomizer(){
   index2 = Int(arc4random_uniform(6))
   dice1.image = UIImage(named: diceImages[index1])
   dice2.image = UIImage(named: diceImages[index2])

Now we have everything ready. But why isn’t it working? We need to insert the function into the IBAction.

@IBAction func rollButtonPressed(_ sender: UIButton) {


The code can be downloaded in GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *