RandomKit是一个快速的框架,使随机数据生成简单简便。
| 分支 | 地位 |
|---|---|
master |
RandomKit可能还与FreeBSD,Android和Windows(在Cygwin下)兼容,但尚未对这些平台进行测试。
Swift软件包管理器是Swift的分散依赖管理器。
将项目添加到您的Package.swift中。
import PackageDescription
let package = Package (
name : " MyAwesomeProject " ,
dependencies : [
. Package ( url : " https://github.com/nvzqz/RandomKit.git " ,
majorVersion : 5 )
]
)导入RandomKit模块。
import RandomKitCocoapods是Objective-C和Swift的集中依赖经理。去这里了解更多。
将项目添加到您的Podfile中。
use_frameworks!
pod 'RandomKit' , '~> 5.2.3'如果您想处于出血边缘,请用以下方式替换最后一行
pod 'RandomKit' , :git => 'https://github.com/nvzqz/RandomKit.git'运行pod install并打开.xcworkspace文件以启动Xcode。
导入RandomKit框架。
import RandomKit迦太基是Objective-C和Swift的分散依赖经理。
将项目添加到您的Cartfile中。
github "nvzqz/RandomKit"
运行carthage update并按照其他步骤进行操作,以便将RandomKit添加到您的项目中。
导入RandomKit框架。
import RandomKit通过运行benchmark.sh可以轻松地对RandomKit的各种组件进行基准测试。
./benchmark.sh [FLAGS] [PROTOCOLS]使用--help标志以获取有关如何使用它的信息。
注意:默认计数为10000000,如果使用--array标志,这是很多。可以通过将论点传递到--count或-c来改变这一点。
自己尝试一下!下载仓库并打开“ randykit.playground”。
RandomGenerator协议定义了用于生成原始值和随机化缓冲区的基本方法。
所有符合RandomGenerator的类型都有一个静态default值,可以作为生成函数作为inout参数传递。
let value = Int . random ( using : & Xoroshiro . default ) ARC4Random
arc4random功能家族的符号未在Linux和其他平台上导出,因此它们在运行时动态加载。 DeviceRandom
MersenneTwister
Xoroshiro
Xorshift
XorshiftStar
ChaCha
SeedableRandomGenerator用于可以与某些相关Seed类型接种的类型。
RandomBytesGenerator协议是针对专门生成填充许多字节的特定类型的类型。例如, MersenneTwister专门生成UInt64 ,而Xorshift生成UInt32值。
对于单线程程序,可以安全地使用Xoroshiro.default等全局生成器实例作为随机性来源。
对于多线程程序,应使用线程局部实例。这允许不同的线程使用自己的单独的随机发电机,而无需共享可变状态。
在下面的示例中, randomGenerator是每个线程唯一的。
let randomBytes = Xoroshiro . withThreadLocal { randomGenerator in
return [ UInt8 ] ( randomCount : 1000 , using : & randomGenerator )
}线程出口时将线程 - 本地发电机进行交易,因此无需担心清理。
建议不要致电withThreadLocal(_:)或在每个单个时间中获取threadLocal指针。检索线程 - 本地实例会避免开销。
// Bad
let value = Int . random ( using : & Xoroshiro . threadLocal . pointee )
array . shuffle ( using : & Xoroshiro . threadLocal . pointee )
// Good
let threadLocal = Xoroshiro . threadLocal
let value = Int . random ( using : & threadLocal . pointee )
array . shuffle ( using : & threadLocal . pointee )
// Better
Xoroshiro . withThreadLocal { randomGenerator in
let value = Int . random ( using : & randomGenerator )
array . shuffle ( using : & randomGenerator )
}作为快捷方式,您甚至可以直接应用功能作为参数。
let value = Xoroshiro . withThreadLocal ( Int . random )在v4.4.0之前,可以通过实例化给定随机RandomGenerator类型的新种子实例来实现线程安全。问题是每次都会发生不必要的播种。这样,将发电机播种一次,然后可以在以后的点重复使用。
还可以使用发电机的播种版本的快捷方式:
Xoroshiro . withThreadLocalReseeding {
...
}比写作要好得多:
ReseedingRandomGenerator . withThreadLocal ( createdWith : { Xoroshiro . reseeding } ) {
...
}RandomKit非常面向协议,这使其具有非常灵活和模块化的能力。
可以使用RandomGenerator生成随机值的类型的协议。
可以使用RandomGenerator在范围内生成可选随机值的类型的协议。
Int . random ( in : 0 ..< 0 , using : & randomGenerator ) // nil可以使用RandomGenerator在封闭范围内生成随机值的类型的协议。
Int . random ( in : - 100 ... 100 , using : & randomGenerator ) // -79可以从基本值到另一个值的类型的协议,该值不包含。
整数的基本值为0。这意味着呼叫random(to:using:)在负值上的::::::::在负值上将产生随机值或零,而正值将产生随机的正值或零。
如果value == randomBase ,则将随机返回value random(to:using:) 。
Int . random ( to : 2 , using : & randomGenerator ) // Either 0 or 1
Int . random ( to : 0 , using : & randomGenerator ) // Always 0
Int . random ( to : 32 , using : & randomGenerator ) // 15
Int . random ( to : - 5 , using : & randomGenerator ) // -3可以通过另一个值(包含)从基本值生成随机值的类型的协议。
关于RandomToValue的基本价值的相同规则适用于RandomThroughValue 。
实例可以随机元素检索的类型的协议。
[ " Bob " , " Cindy " , " May " , " Charles " , " Javier " ] . random ( using : & randomGenerator ) // "Charles"
" Hello " . characters . random ( using : & randomGenerator ) // "e"像NSArray这样的一些基础类型符合此协议。
实例可以从Range<Index>内检索到的随机元素的类型的协议。
[ 20 , 37 , 42 ] . random ( in : 1 ..< 3 , using : & randomGenerator ) // Either 37 or 42可以改组元素的类型的协议。
// Array
[ 1 , 2 , 3 , 4 , 5 ] . shuffled ( using : & randomGenerator ) // [3, 4, 1, 5, 2]
// Dictionary
[ " a " : 1 , " b " : 2 , " c " : 3 ] . shuffled ( using : & randomGenerator ) // ["a": 3, "b": 1, "c": 2]可变的混shuffled(using:)是shuffle(using:) 。
为了获得更好的Array改组性能,请考虑与shuffle(using:)一起放置。
与Shuffleable类似,除非没有任何元素在其初始位置。
Swift的所有本机整数类型都符合Random-协议。
random(using:)函数创建一个任何值的整数。结果,签名整数可能会导致负值。
Int . random ( using : & randomGenerator ) // An Int within Int.min and Int.max
Int . random ( in : 10 ... 20 , using : & randomGenerator ) // An Int within 10 and 20要创建一个正面签名的整数,请使用random(to:using:)或random(through:using:) 。
Int . random ( to : 1000 , using : & randomGenerator ) // 731
Int . random ( through : 10 , using : & randomGenerator ) // 4可以从任何范围内创建签名的整数,而不会出现溢出的危险。
Int . random ( in : ( . min + 1000 ) ... ( . max - 200 ) , using : & randomGenerator ) // 5698527899712144154默认情况下,从范围内或0.0...1.0生成随机的浮点值。
Double . random ( using : & randomGenerator ) // 0.9813615573117475
Double . random ( in : - 10 ... 10 , using : & randomGenerator ) // -4.03042337718197
Float . random ( in : - 10 ... 10 , using : & randomGenerator ) // 5.167088
Float80 . random ( in : - 10 ... 10 , using : & randomGenerator ) // -3.63204542399198874所有FloatingPoint类型还可以符合随机范围内的RandomInClosedRange 。
Bool.random(using:)有50/50的true 。
如果您需要不同的概率,也会有random(withWeight:using:)有1个weight true可能性。
默认情况下, String , Character和UnicodeScalar在" "..."~"中生成值。
String . random ( ofLength : 10 , using : & randomGenerator ) // "}+[=Ng>$w1"
String . random ( ofLength : 10 , in : " A " ... " z " , using : & randomGenerator ) // "poUtXJIbv["
Character . random ( using : & randomGenerator ) // "#"
Character . random ( in : " A " ... " z " , using : & randomGenerator ) // "s"可以生成一个随机值的数组,用于与init(randomCount:using:) Random类型。
所有其他Random-协议都存在类似的初始化器。
let randoms = Array < Int > ( randomCount : 100 , using : & randomGenerator ) // [8845477344689834233, -957454203475087100, ...]对于符合UnsafeRandom类型,更快的替代方法是init(unsafeRandomCount:using:) 。此初始化器直接填充缓冲区,而不是使用random(using:) 。
let unsafeRandoms = Array < Int > ( unsafeRandomCount : 100 , using : & randomGenerator ) // [759709806207883991, 4618491969012429761, ...]生成1000个随机Int阵列10000计数的基准:
| 发电机 | 时间(以秒为单位) |
|---|---|
Xoroshiro | 0.0271 |
Xorshift | 0.0568 |
XorshiftStar | 0.0319 |
ChaCha | 0.2027 |
MersenneTwister | 0.0432 |
ARC4Random | 0.2416 |
DeviceRandom | 5.3348 |
注意:由于各种因素,结果可能会有所不同。
相同的基准可以运行:
./benchmark.sh --all-generators --array 10000 --count 1000可以在两个Date或TimeInterval值之间生成随机Date 。
默认random(using:)函数返回日期内的Date Date.distantPast和Date.distantFuture 。
Date . random ( using : & randomGenerator ) // "Aug 28, 2006, 3:38 AM"
Date . random ( in : Date . distantPast ... Date ( ) , using : & randomGenerator ) // "Feb 7, 472, 5:40 AM"Decimal类型符合各种Random-协议。
默认情况下, random(using:)函数返回0和1之间的Decimal 。
Decimal . random ( using : & randomGenerator ) // 0.87490000409886706715888973957833129437
Decimal . random ( in : 0.0 ... 10.0 , using : & randomGenerator ) // 6.5464639772070720738747790627821299859可以从整数或双范围内生成随机数,或默认情况下0...100 。
NSNumber . random ( using : & randomGenerator ) // 79
NSNumber . random ( in : - 50 ... 100 , using : & randomGenerator ) // -27
NSNumber . random ( in : 100 ... 200 , using : & randomGenerator ) // 149.6156950363926可以生成随机颜色,具有或没有随机α。
NSColor . random ( using : & randomGenerator ) // r 0.694 g 0.506 b 0.309 a 1.0
NSColor . random ( alpha : true , using : & randomGenerator ) // r 0.859 g 0.57 b 0.409 a 0.047
UIColor . random ( using : & randomGenerator ) // r 0.488 g 0.805 b 0.679 a 1.0
UIColor . random ( alpha : true , using : & randomGenerator ) // r 0.444 g 0.121 b 0.602 a 0.085由于CGFloat符合FloatingPoint ,因此它像Double和Float一样,符合RandomInClosedRange 。
CGFloat . random ( using : & randomGenerator ) // 0.699803650379181
CGFloat . random ( in : 0 ... 100 , using : & randomGenerator ) // 43.27969591675319可以从x和y的范围内生成一个随机点。
CGPoint . random ( using : & randomGenerator ) // {x 70.093 y 95.721}
CGPoint . random ( xRange : 0 ... 200 , yRange : 0 ... 10 , using : & randomGenerator ) // {x 73.795 y 0.991}可以从范围内生成随机大小的宽度和高度。
CGSize . random ( using : & randomGenerator ) // {w 3.744 h 35.932}
CGSize . random ( widthRange : 0 ... 50 , heightRange : 0 ... 400 , using : & randomGenerator ) // {w 38.271 h 239.636}可以从x,y,宽度和高度内的范围内生成一个随机的矩形。
CGRect . random ( using : & randomGenerator ) // {x 3.872 y 46.15 w 8.852 h 20.201}
CGRect . random ( xRange : 0 ... 50 ,
yRange : 0 ... 100 ,
widthRange : 0 ... 25 ,
heightRange : 0 ... 10 ,
using : & randomGenerator ) // {x 13.212 y 79.147 w 20.656 h 5.663}可以从DX和DY的范围内生成随机向量。
CGVector . random ( using : & randomGenerator ) // {dx 13.992 dy 89.376}
CGVector . random ( dxRange : 0 ... 50 , dyRange : 0 ... 10 , using : & randomGenerator ) // {dx 35.224 dy 13.463}Károly的Bigint库的RandomKit扩展名可在RandomKitBigint中获得。
RandomKit及其资产根据MIT许可发布。资产可以在assets分支中找到。
该项目的一部分利用Matt Gallagher编写的代码,并与MIT许可结合使用,并在此处找到了该代码。