roll dice 10 times and print each result with its sum in swift

var rolls = [Int]()
var sum = 0

for _ in 1...10 {
    let roll = Int.random(in: 1...6)
    sum += roll
    print("Roll result: \(roll), sum so far: \(sum)")

print("All rolls: \(rolls), total sum: \(sum)")
232 chars
12 lines

In this code, we create an empty array called rolls to store the individual results of each dice roll, and set the initial sum to zero. We then use a for loop to iterate 10 times, generating a random integer between 1 and 6 (inclusive) using the random(in:) method. We add the result of each roll to the array and to the running total sum, and print the current roll result and the sum so far to the console.

After the loop completes, we print out the entire array of rolls and the final sum.

gistlibby LogSnag