I'm able to pick a random number for my items in my game but is it possible to pick a random number between 2 numbers?
so instead of
let number = (arc4random_uniform(100))
I would like something like this:
let number = (arc4random_uniform(10...20))
or something like that? Now if I get a weapon drop it can be everything in my list. This way I could make it so that only the first few would have a drop for a specific monster or at higher level they would drop better weapons and not the low levels anymore.
To generate a random number in Swift, use Int. random() function. Int. random() returns a number, that is randomly selected, in the given range.
Method 1: Using Math. random() function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive). This random number can then be scaled according to the desired range.
Note: If you want to generate random number based on a list, you can use this formula =INDEX($I$2:$I$7, RANDBETWEEN(1, 6)), and press Enter key.
int randomNumber = ( int )( Math. random() * 9999 ); if( randomNumber <= 1000 ) { randomNumber = randomNumber + 1000; Math. random() is a method that generates a random number through a formula.
Xcode 11 • Swift 5.1 or later
extension Range where Bound: FixedWidthInteger {
var random: Bound { .random(in: self) }
func random(_ n: Int) -> [Bound] { (0..<n).map { _ in random } }
}
extension ClosedRange where Bound: FixedWidthInteger {
var random: Bound { .random(in: self) }
func random(_ n: Int) -> [Bound] { (0..<n).map { _ in random } }
}
Note: For older Swift versions check the edit history
Usage:
(10...20).random // 16
(0...1).random(10) // [0, 1, 0, 0, 1, 1, 1, 1, 1, 0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With