Swift - 使用arc4random()、arc4random_uniform()取得隨機數


arc4random()這個全局函數會生成9位數的隨機整數

1,下面是使用arc4random函數求一個1~100的隨機數(包括1和100)

let temp:Int = Int(arc4random()%100)+1
        print(temp)

2,下面是使用arc4random_uniform函數求一個1~100的隨機數(包括1和100)

let temps:Int = Int(arc4random_uniform(100))+1
        print(temps)

arc4random_uniform會隨機返回一個0到上界之間(不含上界)的整數。以2為上界會得到0或1,像投硬幣一樣

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM