I need a function like Android's SecureRandom() in cocos2dx c++

I need a function like Android's SecureRandom() in cocos2dx c++
0.0 0

#1

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.

Pls check this link: https://paragonie.com/blog/2016/05/how-generate-secure-random-numbers-in-various-programming-languages#c-csprng


#2

Why mersenne random isn’ enough?
For secure random you need some secure hardware. For example some HSM, or some crypto chip.
HSM wiki


#3

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?


#4

Do this when game starts srand(time(0));
For more details read here : https://stackoverflow.com/questions/4736485/srandtime0-and-random-number-generation


#5

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 :slight_smile: .


#6

How about c++ std?

Example

This program simulates throwing 6-sided dice.

#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';
}

http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution


#7

Use rand(), make your own function using it.

Never used. Will it work in all platforms? c++11


#8

ccrandom_0_1() is actually rand() btw


#9

cocos2d-x already based on c++11 since v3.0


#10

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.


#11

@smitpatel88 Sorry, I misunderstood it. According to http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution , it shows (since C++11), hope this help.


#12

Thanks for your input dear fellow devs, so here I got 2 options:

  1. mersenne (c++ std)
  2. srand(time(0)); and rand();
    Which will be the best for all platforms? Mainly iOS and Android.

#13

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.