Taran Goel
Taran Goel

Reputation: 468

Negative random number in Swift

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

Answers (5)

Imanou Petit
Imanou Petit

Reputation: 92549


#1. Int's random(in:) method

With 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

#2. Int's random(in:using:) method

If 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

#3. Range's randomElement() and randomElement(using:) methods

Range 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

leogdion
leogdion

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

David Rysanek
David Rysanek

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

Dharmbir Singh
Dharmbir Singh

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

iStrange
iStrange

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

Related Questions