dongxiusuo9881 2019-06-14 15: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 15: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

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

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码