so my goal in this codebit is to randomly roll two dice and as we all know your regular die only has 6 sides so I imported Foundation for access to arc4random_uniform(UInt32). I attempted using the range of (1..7) to avoid randomly getting 0 however that returned an error which I didn't enjoy too much. I tried to do this:
dice1 = arc4random_uniform(UInt32(1..7))
however that returned
Could not find an overload for 'init' that accepts the supplied arguments
I hope that this is enough information for you amazing debs out there to help me :)
Please note I am just doing this in a playground to practice swift. It isn't imperative that I learn how to do this; it's just me tinkering before I jump into building actual apps :D
//imports random number function
import Foundation
//creates data storage for dice roll
var dice1: UInt32 = 0
var dice2: UInt32 = 0
//counter variable
var i = 0
//how many times snake eyes happens
var snakeeyes = 0
//how many times a double is rolled
var `double` = 0
//rolls dice 100 times
while i < 100{
//from here
//sets dice roll
This returns an error of 'Range $T3' is not convertible to UInt32
dice1 = arc4random_uniform(1..7) dice2 = arc4random_uniform(1..7)
//checks for snake eyes
if dice1 == 1 && dice2 == 1 {
snakeeyes = snakeeyes + 1
}
//checks for doubles
if dice1 == dice2{
`double` = `double` + 1
}
//increases counter
i = i + 1
//to here
}
println("You got Snake Eyes \(snakeeyes) times.")
println("You got Doubles, \(`double`) times.")
If You want i create that for random numbers. this is extension of number Int and Double, Float
USE :
Swift 3/4:
I believe you should do
to get the range 1 - 6. I don't do iOS objective C nor have I any knowledge on swift-language though. The random method should return a value between 0 and 5, and + 1 will make it a value between 1 and 6.
If you need a range between lets say 10 - 30 then just do
Quite a few good answers, but I just wanted to share my personal favourite Swift random number generation function for positive integers:
Swift 2
Swift 3
Here's a quick update for Swift 3 and, as a bonus, it now works for any value type that conforms to the SignedInteger protocol - much more convenient for core data applications that need to specify Int16, Int32 etc. As a quick note, if you really need it to work on unsigned integers as well, just copy the entire function then replace
SignedInteger
withUnsignedInteger
andtoIntMax()
withtoUIntMax()
.Swift 4
Thanks to the removal of toIntMax() in Swift 4, we now have to use a different means of converting to a common integer type. In this example I'm using Int64 which is large enough for my purposes, but if you're using unsigned integers or have an Int128 or Int256 custom type you should use those.
One more, for the total random-phile, here's an extension that returns a random element from any
Collection
type object. Note this uses the above function to generate its index so you will need both.Usage
returns a random number between 1 and 6.
returns a number between 50 and 100 inclusive. Naturally you can replace the values of 50 and 100 with whatever you like.
Swift:
I modified @DaRk-_-D0G's answer to work with Swift 2.0