random.org的随机数调用api

via: https://api.random.org/json-rpc/1/basic

这货号称是真随机, 看起来好屌的样子

API 使用一般都是两步

  1. 申请key

    点右上角那个Get Beta Key即可, 需要填个邮箱接收key

  2. HTTP request参数

    直接放码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    import requests
    import json
    
    url = "https://api.random.org/json-rpc/1/invoke"
    
    payload = {
        "jsonrpc": "2.0",
        "method": "generateIntegers",
        "params": {
            "apiKey": "4125d851-2fed-41c6-8fb4-9aa8ccb20bd7",  # key
            "n": 100,  # 希望获取多少个随机数
            "min": 100,  # 随机数从何开始
            "max": 150,  # 从何结束
            "replacement": True,
            "base": 10  # 进制
        },
        "id": 18289  # 暂时目测5位随机数
    }
    
    headers = {
        'content-type': "application/json",
        'cache-control': "no-cache"
    }
    
    response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
    print(json.loads(response.text))
    

    得到的正确响应大概是这样:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    {
        "jsonrpc": "2.0", 
        "result": {
            "random": {
                "data": [
                    108, 
                    125, 
                    133, 
                    149, 
                    112, 
                    101, 
                    123, 
                    101, 
                    103, 
                    135
                ], 
                "completionTime": "2017-07-27 09:49:04Z"
            }, 
            "bitsUsed": 57, 
            "bitsLeft": 22138, 
            "requestsLeft": 988, 
            "advisoryDelay": 210
        }, 
        "id": 18289
    }
    

    具体异常直接看官网文档

加载评论