Cocos2d-x on the problem of random number acquisition, cocos2d-x Random Number

Source: Internet
Author: User

Cocos2d-x on the problem of random number acquisition, cocos2d-x Random Number

We used to get a random number when playing a game.

1 rand()%(end-start+1)+start;  

The end and start parameters are used to set the random number range. To obtain a random number that is close to the real number, you need to set a seed. The common method is to use the current time as the seed, A way to get time is provided in the cocos2d-x, And the get time is more accurate

1 cc_timeval psv;2     CCTime::gettimeofdayCocos2d(&psv, NULL);3     unsigned long int seed = psv.tv_sec*1000 + psv.tv_usec/1000;

The obtained time can be accurate to milliseconds.

The requirement of the project is to obtain a set of random numbers, which requires a loop to obtain a large number of random numbers. In the beginning, we did this:

 1 //CardDispatcher.cpp 2  3 int CCardDispatcher::getRandomNumber(){ 4         cc_timeval psv; 5     CCTime::gettimeofdayCocos2d(&psv, NULL); 6     unsigned long int seed = psv.tv_sec*1000 + psv.tv_usec/1000; 7     srand(seed); 8  9     int randNumber = random(0, 10);10     return randNumber;11 }12 13 int CCardDispatcher::random(int start, int end){14     15     return CCRANDOM_0_1()*end+start;16 17 }18 19 //MainGameLayer.cpp20 21 bool MainGameLayer::init(){22     CCLayer::init();23     this->schedule(schedule_selector(MainGameLayer::getCardNumber), 1.0);24     return true;25 }26 27 void MainGameLayer::getCardNumber(float dt){28     int randomNumber = CCardDispatcher::shareDispatcher()->getRandomNumber();29     CCLOG("%d", randomNumber);30 }

When debugging, we found that the random number obtained was quite regular ......

At that time, I felt very strange. I checked it online for a long time and did not find any problems (maybe I did not take a closer look at the tutorial, haha ). Later, I suddenly felt that I should put the set seed out of the loop, and then the source code was changed as follows:

 1 //CardDispatcher.cpp 2  3 int CCardDispatcher::getRandomNumber(){ 4     int randNumber = random(0, 10); 5     return randNumber; 6 } 7  8 int CCardDispatcher::random(int start, int end){ 9     10     return CCRANDOM_0_1()*end+start;11 12 }13 14 15 //MainGameLayer.cpp16 17 bool MainGameLayer::init(){18     CCLayer::init();19 20     cc_timeval psv;21     CCTime::gettimeofdayCocos2d(&psv, NULL);22     unsigned long int seed = psv.tv_sec*1000 + psv.tv_usec/1000;23     srand(seed);24     this->schedule(schedule_selector(MainGameLayer::getCardNumber), 1.0);25     return true;26 }27 28 void MainGameLayer::getCardNumber(float dt){29     int randomNumber = CCardDispatcher::shareDispatcher()->getRandomNumber();30     CCLOG("%d", randomNumber);31 }

Debugging is successful.

So, set the position of the seed to be placed outside the loop .....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.