nikoo
V2EX  ›  问与答

nginx 有没有随机数?

  •  
  •   nikoo · Jan 29, 2020 · 4650 views
    This topic created in 2326 days ago, the information mentioned may be changed or developed.
    比如:
    location / {
    if(百分之一几率){
    return 200 'lucky!';
    }
    return 200 '...';
    }

    这个“百分之一几率”的判断怎么写?
    7 replies    2020-01-29 23:17:15 +08:00
    ysc3839
        1
    ysc3839  
       Jan 29, 2020   ❤️ 1
    http://nginx.org/en/docs/http/ngx_http_split_clients_module.html
    有找到 A/B 测试用的,但是这里的 Example 是根据 IP 地址来区分的。
    2wex
        2
    2wex  
       Jan 29, 2020   ❤️ 2
    给 nginx 安装一个 lua 扩展,然后用 Lua 写随机
    optional
        3
    optional  
       Jan 29, 2020   ❤️ 1
    $request_id 勉强算随机
    annoy1309
        4
    annoy1309  
       Jan 29, 2020
    取几个种子(时间+ip+盐之类)多次(大于 5 ) sha256 以后,勉强可以算弱随机?
    nikoo
        5
    nikoo  
    OP
       Jan 29, 2020
    @optional
    $request_id
    unique request identifier generated from 16 random bytes, in hexadecimal

    是一个 16 进制随机 ID,如何用这个 ID 实现例如几分之一的随机呢?谢谢
    optional
        6
    optional  
       Jan 29, 2020   ❤️ 1
    @nikoo 最靠谱的还是上面说的来个扩展搞定问题,非要使用$request_id 不如从正则表达式下手, 比如 a 开头是 1/16,aa 开头是 1/256
    nikoo
        7
    nikoo  
    OP
       Jan 29, 2020
    @optional 好办法啊!我觉得这个方法很赞!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2966 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:47 · PVG 12:47 · LAX 21:47 · JFK 00:47
    ♥ Do have faith in what you're doing.