当前位置:网站首页>Using swift language features, write a pseudo-random number generator casually
Using swift language features, write a pseudo-random number generator casually
2022-07-03 12:38:00 【iWillook】
//
// main.swift
// Ultimate
//
// Created by Mewlan Musajan on 2/13/22.
//
// Apple Inc. “The Swift Programming Language (Swift 5.5).” Apple Books. https://books.apple.com/us/book/the-swift-programming-language-swift-5-4/id881256329
protocol RandomNumberGenerator {
func random() -> Double
}
// Linear Congruential Generator
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())")
print("And another one: \(generator.random())")
extension Int {
func repetitions(task: () -> Void) {
for _ in 0..<self {
task()
}
}
}
5.repetitions {
print(generator.random())
}
var randomNumberSet: Set<Int> = []
8.repetitions {
randomNumberSet.update(with: Int(generator.random() * 10))
}
8.repetitions {
if var someNumber = randomNumberSet.popFirst() {
if someNumber == 0 {
someNumber = 1
}
print(someNumber, terminator: "")
}
}
print()边栏推荐
- 在网上炒股开户可以吗?资金安全吗?
- Fundamentals of concurrent programming (III)
- If you can't learn, you have to learn. Jetpack compose writes an im app (II)
- [embedded] - Introduction to four memory areas
- Eureka自我保护
- Solve the problem of VI opening files with ^m at the end
- Integer string int mutual conversion
- What is more elegant for flutter to log out and confirm again?
- MySQL time zone solution
- 剑指Offer04. 二维数组中的查找【中等】
猜你喜欢
随机推荐
Sqoop1.4.4原生增量导入特性探秘
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
wpa_ cli
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
elastic_ L01_ summary
Pki/ca and digital certificate
232. Implement queue with stack
I'm too lazy to write more than one character
初入职场,如何快速脱颖而出?
Unicode查询的官方网站
Adult adult adult
Swift Error Handling
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
JVM内存模型
OpenStack节点地址改变
十條職場規則
记录自己vulnhub闯关记录
It feels great to know you learned something, isn‘t it?








