dongzhi5587 2017-11-07 19:48
浏览 55
已采纳

在php中制作一个coinflip(可公平)

so I'm trying to create website with a coinflip system (Its just a small project I'm doing in my free time) but I don't really know where to begin. I need to make it in php (so its in the backend) and I need it to be provably fair (so I can prove that it is legit). What I've found out is that I need to use something like sh256 but I also heard that its pretty out dated and can be easily cracked. Also if it matters it's a site with a steam login system so I plan on being able to join 1v1's with others steam users not just a person sitting besides me or something (not just 1 button is what I mean hehe).

EDIT: I have googled it and tried asking people I know and etc if they knew anything but nothing was any good.

Thanks in advance

-Eiríkur

  • 写回答

1条回答 默认 最新

  • duanjiushu5063 2017-11-07 19:59
    关注

    This is a simple way to get a random coin toss result:

    $result = array("heads", "tails")[random_int(0,1)];
    

    First, we make an array, which will be our choices. array("heads, "tails") means we will always get one of those 2 results. Next, in the same line, we can select a single element to actually assign to the $result variable from the array we made previously. We can use random_int(min, max) to generate that number.

    Note: random_int() generates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game. http://php.net/manual/en/function.random-int.php

    As a bonus, you could add more elements to this array, and then just increase the max value in random_int(), and it will work. You could make this more dynamic as-well by doing it like this:

    $choices = ["heads", "tails", "Coin flew off the table"];
    $result = $choices[random_int(0, count($choices)-1];
    

    With the above code, you can have as many choices as you'd like!


    Testing

    I ran this code 50,000 times, and these were my results.

    Array
    (
        [heads] => 24923
        [tails] => 25077
    )
    

    And I ran this code 100,000 times, these were my results:

    Array
    (
        [tails] => 49960
        [heads] => 50040
    )
    

    You can play around with this here, to check out results:

    https://eval.in/894945

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP