generate a random number between 0 and 1 in swift

To generate a random number between 0 and 1 in Swift, you can use the random method of the Double type, which returns a random double value between 0.0 (inclusive) and 1.0 (exclusive):

main.swift
let randomNumber = Double.random(in: 0..<1)
print(randomNumber)
64 chars
3 lines

Alternatively, you can use the drand48 function from the Darwin module (imported automatically in Swift):

main.swift
let randomNumber = drand48()
print(randomNumber)
49 chars
3 lines

Both methods will generate a random number between 0 and 1 with uniform probability distribution.

gistlibby LogSnag