In the documentation,there’s this example of generating a random number:
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).**truncatingRemainder**(dividingBy:m))
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
print(“Here’s a random number: (generator.random())”)
// Prints “Here’s a random number: 0.37464991998171”
print(“And another one: (generator.random())”)
// Prints "And another one: 0.729023776863283
I am really not understanding the difference between a Truncating Remainder and a normal Remainder and how the random number is generated.
Is there a way when iterating through the vehicles array to get other parameters from the object not in the protocol? I can see using this technique for example in a tableview showing a list of all the vehicles (even though they are different objects). But if the user taps on the cell with the vehicle could they then change one of its non protocol parameters?
Thanks, Mike
yes. you just need to add an if statement to check if its the type of model object you’re looking for. lets say that you are looking for a Car in the vehicle array. It would look like this:
for vehicle in vehicles {
if let car = vehicle as? Car {
//add implementation here
}
}