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許可結合使用,並在此處找到了該代碼。