• Home
  • About
  • Our Apps
  • Learn
  • Contact
Login

Register

Login
Seemu Apps Seemu Apps
  • Home
  • About
  • Our Apps
  • Learn
  • Contact

Generating a Random Number in Swift

Home ios Generating a Random Number in Swift

Generating a Random Number in Swift

Apr 13, 2016 | Posted by Andrew | ios, swift, tutorial, xcode |

Generating a random number in swift is easy. This tutorial will cover generating a random int, double, float and CGFloat using simple functions.

To generate a random integer use the following function

    func randomInt(min: Int, max: Int) -> Int {
        return min + Int(arc4random_uniform(UInt32(max - min + 1)))
    }

Then you simply can generate a random integer (Int) as follows, the rInt constant will be assigned a random number and will print out any number from 1-6 to the console. In our source code we have it in the viewDidLoad() so we can verify the number is being generated.

        let rInt = randomInt(1, max: 6)
        print(rInt) // Will print a random ing from 1-6 to the console

You can easily change the min and max number to generate a random number in any range of numbers you desire.

To generate a random Double, Float or CGFloat use the following functions.

    func randomDouble(min: Double, max: Double) -> Double {
        return (Double(arc4random()) / 0xFFFFFFFF) * (max - min) + min
    }
    
    func randomFloat(min: Float, max: Float) -> Float {
        return (Float(arc4random()) / 0xFFFFFFFF) * (max - min) + min
    }
    
    func randomCGFloat(min: CGFloat, max: CGFloat) -> CGFloat {
        return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (max - min) + min
    }

And you just simply assign a variable or constant to call the function with the minimum and maximum value you want.

        let randDouble = randomDouble(1, max: 10)
        let randFloat = randomFloat(1, max: 10)
        let randCGFloat = randomCGFloat(1, max: 10)
        
        print(randDouble)
        print(randFloat)
        print(randCGFloat)

The source code has all of this in one function and will output examples to the console if you wish to take a look at it all in action.

DownloadSourceCode

Tags: cgfloatdoublefloatintintegernumberrandom
0
Share

About Andrew

Andrew is a 24 year old from Sydney. He loves developing iOS apps and has done so for two years.

You also might be interested in

Swift check if an element is in an array

Apr 12, 2016

Checking if an element is an an array in swift[...]

Convert String to Number (Integer, Float, Double)

Apr 21, 2016

Converting a string to a number is simple thanks to[...]

Swift Extensions – speed up app development

May 24, 2017

Swift Extensions are quite a powerful feature and Swift and[...]

Welcome

Hi I am Andrew and welcome to Seemu Apps! Have a look around, we provide tutorials for primarily iOS apps.
Bluehost website hosting discount

Seemu’s Studio Setup

Blue Yeti Microphone
Rode Stand
Spider Shock Mount
Mac Keyboard Cover
Screenflow - recording software

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Footer

:)

© 2025 · Your Website. Theme by HB-Themes.

  • Home
  • About
  • Our Apps
  • Learn
  • Contact
Prev Next