TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Random Numbers: arc4random

Random. Our world is full of chance. One day a person wears a red shirt. Then green, blue, white. Our choice is random—in Swift we compute random Ints with a Foundation function.
With arc4random, we can begin a random sequence of numbers. Our program will begin a different sequence each time it is executed.
A first example. This example uses the arc4random function. In Xcode you can scroll through a list of functions by typing a few characters like "ar."

Arc4random: This returns a random number. As we see in the output this number has a large range but is positive.

Here: We use some logic in a repeat-while loop to test our random numbers. We use modulo division to terminate the loop.

Repeat
Swift program that uses arc4random import Foundation // Use infinite "repeat while true" loop. repeat { // Get random number with arc4random. let number = arc4random() print("The computer selected: \(number)") // Check for multiple of 10. if number % 10 == 0 { print("Divisible by 10, stopping loop.") break } if number % 5 == 0 { print("Divisible by 5, you are a winner.") } } while true Output The computer selected: 1580707251 The computer selected: 2169372518 The computer selected: 2188614733 The computer selected: 2102187726 The computer selected: 1259434845 Divisible by 5, you are a winner. The computer selected: 4203979483 The computer selected: 3751678511 The computer selected: 1143995553 The computer selected: 1399438296 The computer selected: 4189143210 Divisible by 10, stopping loop.
Arc4random, seed. With arc4random, a different sequence is generated each time a program is run. No special seed value must be specified.

Important: With arc4random, programs will not have the same results each time they are run.

Swift program that shows random numbers import Foundation // With arc4random we do not need to seed the random number generator. // ... A difference sequence is generated each time. print("Test") for i in 0...3 { print(arc4random()) } Output Test 2350561563 3189423288 1041162380 2422311100 (Second run) Test 1179004133 1484905321 2378084349 2120303966
With helpful Foundation methods, we generate random numbers in Swift. Arc4random is used for best pseudo-random results. It does not need a special seed.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf