generate a random number in swift

In Swift, we can generate a random number using the random(in:) function. This function takes a range as an argument and returns a random number within that range.

Here's an example of generating a random number between 1 and 100:

main.swift
let randomNumber = Int.random(in: 1...100)
print(randomNumber)
63 chars
3 lines

This will print a random integer between 1 and 100.

We can also generate a random number within a specific range by using the random(in:) function with a different range:

main.swift
let randomNumber = Double.random(in: -10.0...10.0)
print(randomNumber)
71 chars
3 lines

This will print a random double between -10.0 and 10.0.

gistlibby LogSnag