Generate random numbers with a given distribution

前端 未结 4 474
悲&欢浪女
悲&欢浪女 2020-11-28 13:39

Check out this question:

Swift probability of random number being selected?

The top answer suggests to use a switch statement, which does the job. However, i

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 14:32

    This seems like a good opportunity for a shameless plug to my small library, swiftstats: https://github.com/r0fls/swiftstats

    For example, this would generate 3 random variables from a normal distribution with mean 0 and variance 1:

    import SwiftStats
    let n = SwiftStats.Distributions.Normal(0, 1.0)
    print(n.random())
    

    Supported distributions include: normal, exponential, binomial, etc...

    It also supports fitting sample data to a given distribution, using the Maximum Likelihood Estimator for the distribution.

    See the project readme for more info.

提交回复
热议问题