a loop that gives me random coordinates in swift

You can generate random coordinates by using arc4random_uniform() function which returns random number between 0 and a given value. Here is an example of how you can use it to generate random coordinates within a loop:

main.swift
for _ in 1...10 { // loop 10 times
    let randomX = Double(arc4random_uniform(100)) // generate random number between 0 and 99
    let randomY = Double(arc4random_uniform(100)) // generate random number between 0 and 99
    let coordinate = (x: randomX, y: randomY) // create tuple with random coordinates
    print(coordinate) // print the coordinate tuple
}
361 chars
7 lines

This code will generate and print 10 sets of random coordinates within the range of (0,0) to (99,99). You can modify the range by changing the argument of arc4random_uniform() function.

related categories

gistlibby LogSnag