mirror of
https://github.com/samsonjs/SwiftBatteries.git
synced 2026-03-25 09:25:51 +00:00
move randomInt to Utils
This commit is contained in:
parent
3f0daeded7
commit
0bcd36dda7
2 changed files with 15 additions and 16 deletions
|
|
@ -1,11 +1,5 @@
|
|||
import Foundation
|
||||
|
||||
#if os(Linux)
|
||||
import Glibc
|
||||
#else
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
public extension CollectionType {
|
||||
|
||||
typealias T = Generator.Element
|
||||
|
|
@ -32,16 +26,6 @@ public extension CollectionType {
|
|||
|
||||
}
|
||||
|
||||
/// Returns a random integer between 1 and max
|
||||
func randomInt(max: Int) -> Int {
|
||||
#if os(Linux)
|
||||
let n = random() % max
|
||||
#else
|
||||
let n = arc4random_uniform(UInt32(max))
|
||||
#endif
|
||||
return 1 + Int(n)
|
||||
}
|
||||
|
||||
public extension CollectionType where Index: RandomAccessIndexType {
|
||||
|
||||
/// Returns a random element, or nil if the collection is empty
|
||||
|
|
|
|||
15
Sources/Utils.swift
Normal file
15
Sources/Utils.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#if os(Linux)
|
||||
import Glibc
|
||||
#else
|
||||
import Darwin
|
||||
#endif
|
||||
|
||||
/// Returns a random integer between 1 and max, inclusive.
|
||||
public func randomInt(max: Int) -> Int {
|
||||
#if os(Linux)
|
||||
let n = random() % max
|
||||
#else
|
||||
let n = arc4random_uniform(UInt32(max))
|
||||
#endif
|
||||
return 1 + Int(n)
|
||||
}
|
||||
Loading…
Reference in a new issue