{

Random Generator in swift with example


This tutorial shows multiple ways to generate random generators in Swift with examples.

Generator Random Number between 1 and 10

In Swift, Each primitive type has a static random function.

  • Int.random
  • Float.random
  • Double.random
  • Bool.random()

These types of functions returns a random value. Let’s see an example to generate a random number for the Int class.

static func random(in: Range<Self>) -> Self

The range contains below types.

  • m...n which is inclusive of m and n
  • m..<n that is inclusive of m and exclusive n

Here is an example to generate random numbers between 1 and 10.

import Foundation

let randomValue = Int.random(in: 1...10)
print(randomValue)  //6 
let randomValue1 = Int.random(in: 1..<11)
print(randomValue1)  //8

Generate Random Boolean values in swift

Bool contains true and false values.

It has a random() method that returns random true or false values. Here is an example code

import Foundation

let randomValue = Bool.random()
print(randomValue) 
let randomValue1 = Bool.random()
print(randomValue1) 

On running the above code multiple times, It returns random true and false values. Output

true
false

How to generate a Random Number from an array in Swift?

Sometimes, We want to generate random numbers from an array.

Array provides randomElement() that returns a random number from an array.

Here is a code that returns a single random number

var numbers=[1,2,3,4,5,6,7,8,9]
let random=numbers.randomElement()

print( random as Any) 

Let’s see an example to generate multiple random numbers.

First, Shuffle the array using the shuffled() method Next, return a sub-array using the prefix method with the given return array size.

var numbers=[1,2,3,4,5,6,7,8,9]
let random=numbers.shuffled()

print( random.prefix(2))  // [4,8]

Generate Random Number from a Dictionary

Dictionary is a data structure that holds key and values

In this example, Create a Dictionary with type Int and String value, assigned with default data. randomElement() is called return random object from Dictionary. call key and value to return random key and value.

Here is an example

var emps: [Int:String] = [
    1:"john", 2: "eric", 3: "mark", 4: "ram", 5: "sai"
]
let element=emps.randomElement();

print( element?.key as Any) 
print( element?.value as Any) 
Optional(1)
Optional("john")

Other Random Generator methods

The following example explains about generating random numbers in float, double and CGFloat.

import Foundation

let randomFlat = Float.random(in: 0...2.222)
print(randomFlat)  //0.32214516
let randomFloat = Double.random(in: 1..<3.3)
print(randomFloat)  //1.3594377264297153
let cgFloat=CGFloat.random(in: 0..<10.12344)
print(cgFloat)  //3.75995901059995
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.