@@ -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