Reputation: 468
I am developing an iPhone app in swift and there's a requirement where I want to generate NEGATIVE random numbers always.
I know the way to generate random numbers but don't seem to figure out a way to generate negative numbers only.
Upvotes: 3
Views: 5298
Reputation: 92549
Int
's random(in:)
methodWith Swift 5, Int
has a type method called random(in:)
. random(in:)
has the following declaration:
static func random(in range: Range<Int>) -> Int
Returns a random value within the specified range.
The following Playground sample code shows how to use random(in:)
with a range of negative integers in order to get a negative random number from them:
let range = -70 ..< -50
let randomNumber = Int.random(in: range)
print(randomNumber) // prints: -57
let closedRange = Int.min ..< 0
let randomNumber = Int.random(in: closedRange)
print(randomNumber) // prints: -7618449904516816385
Int
also has a type method called random(in:)
that accepts a parameter of type ClosedRange<Int>
:
let closedRange = -70 ... -50
let randomNumber = Int.random(in: closedRange)
print(randomNumber) // prints: -66
Int
's random(in:using:)
methodIf needed, you can use random(in:using:)
to return a random value within the specified range using a custom generator as a source for randomness. random(in:using:)
has the following declaration:
static func random<T>(in range: Range<Int>, using generator: inout T) -> Int where T : RandomNumberGenerator
Returns a random value within the specified range, using the given generator as a source for randomness.
Usage:
let range = -70 ..< -50
var randomNumberGenerator = SystemRandomNumberGenerator() // replace with custom generator
let randomNumber = Int.random(in: range, using: &randomNumberGenerator)
print(randomNumber) // prints: -52
Note that random(in:using:)
has an equivalent random(in:using:)
that accepts a closed range as its parameter:
let range = -70 ... -50
var randomNumberGenerator = SystemRandomNumberGenerator() // replace with custom generator
let randomNumber = Int.random(in: range, using: &randomNumberGenerator)
print(randomNumber) // prints: -58
Range
's randomElement()
and randomElement(using:)
methodsRange
and ClosedRange
get randomElement()
and randomElement(using:)
methods by conforming to Collection
protocol. randomElement()
has the following declaration:
func randomElement() -> Bound?
Returns a random element of the collection.
You may call randomElement()
to select a random element from a range or a closed range. This example picks a number at random from a range of negative numbers:
let range = -70 ..< -50
let randomNumber = range.randomElement()
print(randomNumber) // prints: Optional(-64)
Upvotes: 2
Reputation: 2350
With Swift 4.2, this has been made simpler with the new random methods such as Int.random
. You can read more details about the changes here.
Upvotes: 1
Reputation: 979
This is your function, I believe:
extension Int {
/// Generates a random `Int` within `0...100`
public static func random() -> Int {
return random(0...100)
}
/// Generates a random `Int` inside of the closed interval.
public static func random(interval: ClosedInterval<Int>) -> Int {
return interval.start + Int(arc4random_uniform(UInt32(interval.end - interval.start + 1)))
}
}
Usage example:
Int.random(-10...0)
It is taken from RandomKit library - it looks very useful for various purposes.
Upvotes: 5
Reputation: 17535
Please try to use this one
let lowerValue = -100
let upperValue = 0
let result = Int(arc4random_uniform(UInt32(upperValue - lowerValue + 1))) + lowerValue
print(result)
Output
-81
Upvotes: 12
Reputation: 61
var randomNumber = -1 * Int(arc4random_uniform(UInt32(5)))
5 will make sure that the random number is generated though zero to five.
Upvotes: 4