Hello Devs,
I need a function like Android’s SecureRandom() in cocos2dx c++ to generate a secure random integer. I found libsodium but don’t know how to implement it in cocos2dx. Have anyone tried using libsodium for c++ in cocos2dx ? Or can anyone guide me to implement it? I’m a C++ noob here.
I actually needed to generate random from 1 to 6 for dice rolling. I am using ccrandom_0_1() right now but the users are complaining about it not being random or even saying number is scripted. Sometimes the 6 or 1 repeats a lot or sometimes never comes.
What could the best solution for it? Is mersenne good enough? Can you also send implementation guide?
If I do this srand(time(0)); when the starts then I should be using rand() for getting random number right? Or I can just keep using ccrandom_0_1() as it was (in my case)? Please clarify .
#include <random>
#include <iostream>
int main()
{
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(1, 6);
for (int n=0; n<10; ++n)
//Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
std::cout << dis(gen) << ' ';
std::cout << '\n';
}
lol
I was asking is it included in c++11? or after onwards?
and there are many functions which didn’t work in all platforms perfectly, thats why i asked.
Both are platform independent, both can seeded to get the same results for some cases.
Mersenne twister gives “better balanced” randoms, but for a simple game is mostly irrelevant.
The usage is easy by both version. Use what you want, or you feel comfortable.