stdlib標準庫的常用API

來源:互聯網
上載者:User

標籤:float   使用方法   span   int   標準   post   常用   body   print   

       使用方法 :

                 通過arc4random() 擷取0到x-1之間的整數的代碼如下:

                 int value = arc4random() % x; 


                 擷取1到x之間的整數的代碼如下:

                 int value = (arc4random() % x) + 1; 

 

       2)、CCRANDOM_0_1() cocos2d中使用 ,範圍是[0,1]

       使用方法:

                 float random = CCRANDOM_0_1() * 5; //[0,5]   CCRANDOM_0_1() 取值範圍是[0,1]

 

       3)、random() 需要初始化時設定種子

      使用方法:

                srandom((unsigned int)time(time_t *)NULL); //初始化時,設定下種子就好了。

 

 

rand()實際並不是一個真正的偽隨機數發生器,random()會相對好點,但也不算理想。幸運的是iPhone上還有其他的選擇。個人來說我首選arc4random() ,原因就是它是一個真正的偽隨機演算法,而且範圍是rand()的兩倍。

 

 

-(float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2
{
    int startVal = num1*10000;
    int endVal = num2*10000;

    int randomValue = startVal +(arc4random()%(endVal - startVal));
    float a = randomValue;

    return(a /10000.0);
}

stdlib標準庫的常用API

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.