January 2, 2016

..Geekish..

# Random numbers in swift 2.0 ## Random numbers in swift 2.0

Here is a short description on how to create random numbers in Swift 2.0.

To create random numbers in swift 2.0 you can use the arc4random_uniform function.
Check out the apple documentation for arc4random_uniform function which you can download at swift.org

#### Random numbers in swift 2.0 between 0 and 9

let rand = Int(arc4random_uniform(10))

So as you can see we type in 10 into the function. But the function will never return 10, it is up to 10 but never 10.

#### Random numbers in swift 2.0 between 0 and 10

let rand = Int(arc4random_uniform(11)) or you can type it like this if you find that easier.
let rand = Int(arc4random_uniform(10+1))

#### Random numbers in swift 2.0 between 5 and 10

let rand = Int(arc4random_uniform(6)) + 5 // This will return numbers between 5 and 10 (including 10)

Just remember when inserting the max limit into the function. It is the max range, but it always starts at zero.

If you need to create more than one random number in your program. Maybe you should consider to write a function for it. Here is an example for a function you could use in your program.

#### Example random function

func myRandomNumber(Max: UInt32, Min: Int) ->Int {
let rand = Int(arc4random_uniform((Max-UInt32(Min))+1)) + Min
return rand
}

The arc4random_uniform only take UInt32 numbers as parameters. That is why we need to declare Max as UInt32, and cast the Min variable as UInt32 when inside the arc4random_uniform function. We need to use Int to add the Min after the arc4random_uniform function.

A short example on how to use myRandomNumber function to create 10 numbers.

for var x in 0..10 {
let rand = myRandomNumber(10, Min: 5)
print(rand)
}

This code will create 10 random numbers between 5 and 10 and print them to the console.

So this is how you can create random numbers in swift 2.0.

Happy randomising!
{"ft_lang_label":"__label__en","ft_lang_prob":0.78388566,"math_prob":0.99079794,"size":1951,"snap":"2020-10-2020-16","text_gpt3_token_len":505,"char_repetition_ratio":0.18592706,"word_repetition_ratio":0.0508982,"special_character_ratio":0.26345465,"punctuation_ratio":0.09067357,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99282175,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-23T10:58:35Z\",\"WARC-Record-ID\":\"<urn:uuid:2993a3b7-93a6-408b-ab56-7a1af5fd0f53>\",\"Content-Length\":\"32900\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9568960a-7990-4435-bc3f-acba15079ecb>\",\"WARC-Concurrent-To\":\"<urn:uuid:4e884b3a-c364-4ad3-99e1-67ce575b1b14>\",\"WARC-IP-Address\":\"148.251.12.213\",\"WARC-Target-URI\":\"https://soltveit.org/random-numbers-in-swift-2-0/\",\"WARC-Payload-Digest\":\"sha1:AWA2BSM2CZUNJCB3F5GIJ5CTTBVWYWL3\",\"WARC-Block-Digest\":\"sha1:HLKDVNDWAZIVK4W2V24ASOP6HS5M53WF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875145767.72_warc_CC-MAIN-20200223093317-20200223123317-00250.warc.gz\"}"}