V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nikoo
V2EX  ›  问与答

nginx 有没有随机数?

  •  
  •   nikoo · 2020-01-29 21:23:42 +08:00 · 3781 次点击
    这是一个创建于 1520 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如:
    location / {
    if(百分之一几率){
    return 200 'lucky!';
    }
    return 200 '...';
    }

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

    是一个 16 进制随机 ID,如何用这个 ID 实现例如几分之一的随机呢?谢谢
    optional
        6
    optional  
       2020-01-29 23:14:23 +08:00   ❤️ 1
    @nikoo 最靠谱的还是上面说的来个扩展搞定问题,非要使用$request_id 不如从正则表达式下手, 比如 a 开头是 1/16,aa 开头是 1/256
    nikoo
        7
    nikoo  
    OP
       2020-01-29 23:17:15 +08:00
    @optional 好办法啊!我觉得这个方法很赞!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3236 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:59 · PVG 21:59 · LAX 06:59 · JFK 09:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.