on master help sign in
Added linux random numbers
commitb113ab03f7a08103d7c772d8739265ffde30d576 authorilyakooo0 <ilyakooo0@gmail.com> dateFri 30 Nov 2018, 02:10 (8 years ago) parent68a32d7
2 files changed+16−3
Sources/App/Models/SafeDict.swift+11−3 Sources/Run/main.swift+5
Sources/App/Models/SafeDict.swift+11−3
@@ -7,6 +7,14 @@
77
88import Foundation
99
10+func getRandomNum(_ min: Int, _ max: Int) -> Int {
11+ #if os(Linux)
12+ return Int(random() % max) + min
13+ #else
14+ return Int(arc4random_uniform(UInt32(max)) + UInt32(min))
15+ #endif
16+}
17+
1018struct SafeDict<Key: Hashable, Value> {
1119 private var queue = DispatchQueue(label: "soy.iko.safeDict.queue", attributes: .concurrent)
1220
@@ -33,9 +41,9 @@ struct SafeDict<Key: Hashable, Value> {
3341 mutating func removeRandom() -> Key? {
3442 var key: Key?
3543 queue.sync(flags: .barrier) {
36− key = dict.keys.randomElement()
37− if let key = key {
38− dict[key] = nil
44+ if dict.count > 0 {
45+ key = Array(dict.keys)[getRandomNum(0, dict.count)]
46+ dict[key!] = nil
3947 }
4048 }
4149 return key
Sources/Run/main.swift+5
@@ -1,3 +1,8 @@
11import App
22
3+#if os(Linux)
4+srandom(UInt32(time(nil)))
5+#endif
6+
7+
38try app(.detect()).run()