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.swift64 chars3 lines
Alternatively, you can use the drand48
function from the Darwin
module (imported automatically in Swift):
main.swift49 chars3 lines
Both methods will generate a random number between 0 and 1 with uniform probability distribution.
gistlibby LogSnag