dongxiusuo9881 2019-06-14 07:06
浏览 150
已采纳

PHP生成0到36之间的数字可证明是公平的

I'm trying to generate a number from 0 to 36 in php that can be proven that it was fair later.

I've had a google around and found a formula that works, however I'm having a bit of trouble getting it to work in PHP.

roll_hash = hash("sha256", server_seed + "-" + lottery + "-" + roll_id);
roll_hash_first16 = substr(roll_hash, 0, 16);
roll_lucky_number = hex_to_uint64(roll_hash_first16) % 15;

is the formula I've found, however I can't get it to work in PHP.

This is the PHP code I've tried however it doesn't do what I need it to do.

$server_seed = "577701c29f6e4a409b8a607cb95c79c943146dc7dff3eb6894a34837076e7365";
$salt = "fe5e5c41b";
$roll_id = 0;

$roll_hash = hash("sha256", $server_seed + "-" + $salt + "-" + $roll_id);
$roll_hash_first16 = substr($roll_hash, 0, 36);
$roll_lucky_number = hexdec($roll_hash_first16) / 35;

This code will just generate something like "5.3687216943283E+41"

I need it to generate a plain number from 0 to 36 and which should be the same whenever the server seed, salt and roll_id are the same.

  • 写回答

1条回答 默认 最新

  • douchen1924 2019-06-14 07:29
    关注

    Here's the corrected version of the PHP code though:

    function generateRandomNumber(string $seed, string $salt, int $roll): int
    {
        $roll_hash = hash("sha256", $seed . "-" . $salt . "-" . $roll);
        $roll_hash_first16 = substr($roll_hash, 0, 16);
        return abs(hexdec($roll_hash_first16) % 37);  
    }
    

    https://3v4l.org/QMgNR

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部