diff --git a/SwiftRandom/Randoms.swift b/SwiftRandom/Randoms.swift index 0794225..9159138 100644 --- a/SwiftRandom/Randoms.swift +++ b/SwiftRandom/Randoms.swift @@ -40,7 +40,11 @@ public extension Int { public extension Int32 { /// SwiftRandom extension public static func random(_ range: Range) -> Int32 { - return random(range.upperBound, range.lowerBound) + #if swift(>=3) + return random(range.lowerBound, range.upperBound) + #else + return random(range.lowerBound, range.upperBound - 1) + #endif } /// SwiftRandom extension diff --git a/SwiftRandomTests/SwiftRandomTests.swift b/SwiftRandomTests/SwiftRandomTests.swift index 9c5914e..517fe9f 100644 --- a/SwiftRandomTests/SwiftRandomTests.swift +++ b/SwiftRandomTests/SwiftRandomTests.swift @@ -33,6 +33,7 @@ class SwiftRandomTests: XCTestCase { /// Tests for extensions applied directly above Swift types. func testTypeExtensions() { XCTAssertNotNil(Int.random()) + XCTAssertNotNil(Int32.random()) XCTAssertNotNil(Date.random()) XCTAssertNotNil(Date.randomWithinDaysBeforeToday(7)) XCTAssertNotNil(CGFloat.random()) @@ -66,7 +67,17 @@ class SwiftRandomTests: XCTestCase { XCTAssertLessThan(randomLessTen, 10) } } - + + func testRandomInt32Range() { + for _ in 0...10 { + + let randomLessTen = Int32.random(0..<10) + + XCTAssertGreaterThanOrEqual(randomLessTen, Int32(0)) + XCTAssertLessThan(randomLessTen, Int32(10)) + } + } + /// Tests generating random a `String` of a specified length. func testRandomStringOfLength() { let precision = 100