当前位置:网站首页>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()边栏推荐
- JVM内存模型
- elastic_ L04_ introduction. md
- The solution to change the USB flash disk into a space of only 2m
- Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
- I'm too lazy to write more than one character
- adb push apk
- Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
- 阿里大于发送短信(用户微服务--消息微服务)
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- 02_ Lock the code, and don't let the "lock" become a worry
猜你喜欢

TOGAF认证自学宝典V2.0

【附下载】密码获取工具LaZagne安装及使用

2021 autumn Information Security Experiment 1 (password and hiding technology)

LeetCode 0556.下一个更大元素 III - 4步讲完

【ManageEngine】IP地址扫描的作用

ES6 standard

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?

Implement verification code verification

(最新版) Wifi分销多开版+安装框架

剑指Offer05. 替换空格
随机推荐
Tensorflow binary installation & Failure
Is it safe to open an account for online stock speculation? Who can answer
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
The solution to change the USB flash disk into a space of only 2m
Public and private account sending prompt information (user microservice -- message microservice)
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Itext7 uses iexternalsignature container for signature and signature verification
temp
Airflow installation jump pit
Sword finger offer07 Rebuild binary tree
Sword finger offer09 Implementing queues with two stacks
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
实现验证码验证
2.8 overview of ViewModel knowledge
Eureka自我保护
TOGAF认证自学宝典V2.0
The best shortcut is no shortcut
Alibaba is bigger than sending SMS (user microservice - message microservice)