版权声明:转载请说明出处 https://blog.csdn.net/weixin_39842528/article/details/83030625
除了我之前提到的eosbet游戏合约中,使用的随机生成器;
接下来介绍另一个合约来实现随机生成器,废话不多说,直接贴代码
// Linear Congruential Generator
class random_gen {
private:
static random_gen instance;
const uint32_t a = 1103515245;
const uint32_t c = 12345;
uint64_t seed = 0;
public:
static random_gen& get_instance(account_name player) {
if (instance.seed == 0) {
instance.seed = current_time() + player;
}
return instance;
}
uint32_t range(uint32_t to) {
checksum256 result;
sha256((char *)&seed, sizeof(seed), &result);
seed = result.hash[1];
seed <<= 32;
seed |= result.hash[0];
return (uint32_t)(seed % to);
// old implementation
// seed = (a * seed + c) % 0x7fffffff;
// return (uint32_t)(seed % to);
}
};
更多高级合约开发请添加微信学习交流