Search code examples
iosswiftrandomgamekit

What does the message ' do not match any available overloads' mean?


In the playground, I'm trying to generate random numbers for a normal distribution using the following code:

import UIKit
import GameplayKit

var mu: Double = 0
var random: Double = 0

for i in 1...1000 {
    let random = GKRandomSource()
    mu = GKGaussianDistribution(randomSource: random, mean: Float(0.0), distribution: Float(1.0))
    print(mu)
}

This will not compile, I get the following error message: "Argument labels '(randomSource:, mean:, distribution:)' do not match any available overloads"

What does this mean and how can I correct the code?


Solution

  • There are a couple of issues with your code, the parameter is called deviation, not distribution, and the Distribution object itself does not return a random number.

    Try this instead:

    import GameplayKit
    let random = GKRandomSource()
    let dist = GKGaussianDistribution(randomSource: random, mean: 0, deviation: 1.0)
    
    for _ in 1...1000 {
        let mu = dist.nextUniform()
        print(mu)
    }