GameCorder.net

このエントリーをはてなブックマークに追加

How to create random value with utilize function in cocos2dx

cocos2dx has util function for creating random value.
In namespace cocos2d::RandomHelper
Let's see example.


// 0 to 4 in i
int i = cocos2d::RandomHelper::random_int(0, 4);
		

Function random_int first arg is min value
Second arg is max value
Then this create 0 between 4

random_int use c++ std function uniform_int_distribution


template
static inline T random_int(T min, T max) {
    std::uniform_int_distribution dist(min, max);
    auto &mt = RandomHelper::getEngine();
    return dist(mt);
}
		

This is available in cocos2dx 3.15.1