真随机数
一般sdk自带的随机数都是伪随机数
RANDOM.ORG提供了API可以获取真随机数
首先需要申请key
调用API的url https://api.random.org/json-rpc/1/invoke
python测试
>>> base_url='https://api.random.org/json-rpc/1/invoke' >>> import requests >>> req_dict={'jsonrpc':'2.0','method':'generateIntegers','id':'1111'} >>> req_dict['params']={'apiKey':'your_own_key','n':1,'min':1,'max':100} >>> resp = requests.post(base_url,json=req_dict) >>> resp.json() {'jsonrpc': '2.0', 'result': {'advisoryDelay': 40, 'bitsUsed': 7, 'bitsLeft': 249993, 'requestsLeft': 999, 'random': {'completionTime': '2016-01-28 07:14:50Z', 'data': [26]}}, 'id': '1111'} >>> resp_dict = resp.json() >>> resp_dict['result']['random']['data'] [26]
获取一个简单的int类型随机数的API详细说明
里面有个比较重要的optional参数replacement,设置replacement=false表示不可以有重复的
generateIntegers
This method generates true random integers within a user-defined range. Your client must set the method
property of its JSON-RPC request object to generateIntegers
. The request must also contain an id
member, which will be returned in the response.
Required Parameters
The following parameters are mandatory and should be specified in the params
array of the JSON-RPC request:
apiKey
n
min
max
Optional Parameters
The following parameters are optional and can be included in the params
object of your JSON-RPC request if you want functionality that is different from the default:
replacement
(default value true
)true
) will cause the numbers to be picked with replacement, i.e., the resulting numbers may contain duplicate values (like a series of dice rolls). If you want the numbers picked to be unique (like raffle tickets drawn from a container), set this value to false
.base
(default value 10
)2
, 8
, 10
and 16
. This affects the JSON types and formatting of the resulting data as discussed below.Successful Response
If the numbers were generated successfully, RANDOM.ORG returns a JSON-RPC response with the result
property containing an object with the following named values:
random
data
base
10
(or did not specify a base and therefore defaults to 10
), the elements in the array will be integers. Because JSON (according to RFC4627) only allows numbers to be written as decimal, the numbers will be typed as strings if a different base
than 10
was specified in the request. Numbers in any base other than 10
will be padded with leading zeros up to the width required to display the chosen range.completionTime
bitsUsed
bitsLeft
requestsLeft
advisoryDelay
For a successful response, the error
property is absent.
Simple clients may not necessarily need all of the properties in the response. A minimal client could use only the random.data
andadvisoryDelay
properties and ignore the rest of the response.
Error Response
If an error occurred, RANDOM.ORG returns a JSON-RPC response in which the result
property is absent and the error
property contains an error object as described in Error Codes and Messages.
Example 1
The following requests six numbers in the [1,6] range. The replacement
parameter is set to true
, which means the numbers will be picked with replacement, i.e., can contain duplicate values. This makes them suitable for use as dice rolls.
{ "jsonrpc": "2.0", "method": "generateIntegers", "params": { "apiKey": "6b1e65b9-4186-45c2-8981-b77a9842c4f0", "n": 6, "min": 1, "max": 6, "replacement": true }, "id": 42 }
The service responds with the following:
{ "jsonrpc": "2.0", "result": { "random": { "data": [ 1, 5, 4, 6, 6, 4 ], "completionTime": "2011-10-10 13:19:12Z" }, "bitsUsed": 16, "bitsLeft": 199984, "requestsLeft": 9999, "advisoryDelay": 0 }, "id": 42 }
The random
object contains the true random values (in the data
array) produced as well as the completion time. Note that thecompletionTime
specifies UTC time zone (‘Zulu time’) by the letter ‘Z’ after the clock time. Through the other fields in the result
object, RANDOM.ORG also advises how many true random bits were used to satisfy the request (16) and how many bits (199,984) and requests (9,999) are left in the client's quota. It also advises the client that it can go ahead and issue the next request without delay (0 milliseconds).
Example 2
The following requests 52 numbers in the [1,52] range. The replacement
parameter is set to false
, meaning the numbers will be picked without replacement, i.e., duplicates will not occur. This makes them suitable to shuffle a deck of cards.
{ "jsonrpc": "2.0", "method": "generateIntegers", "params": { "apiKey": "6b1e65b9-4186-45c2-8981-b77a9842c4f0", "n": 52, "min": 1, "max": 52, "replacement": false }, "id": 3076 }
The service responds with the following:
{ "jsonrpc": "2.0", "result": { "random": { "data": [ 39, 24, 18, 46, 6, 52, 36, 30, 40, 42, 37, 4, 7, 20, 1, 44, 25, 9, 21, 29, 51, 41, 14, 15, 48, 50, 31, 17, 3, 19, 45, 35, 2, 43, 26, 16, 5, 23, 12, 8, 10, 47, 13, 33, 34, 49, 22, 11, 28, 27, 38, 32 ], "completionTime": "2011-10-10 13:19:12Z", }, "bitsUsed": 296, "bitsLeft": 199704, "requestsLeft": 9999, "advisoryDelay": 2000 }, "id": 3076 }
The random
object contains the true random numbers (in the data
array) produced, as well as the completion time.
The remaining fields in the result
object indicate how many true random bits were used to satisfy the request as well as how many bits and requests are left in the client's quota. The response also advises the client preferably to delay at least two seconds before issuing a new request.
Example 3
The following requests 512 bytes, i.e., numbers in the [0,255] range. No replacement
parameter is given, which means the service will use the default value of true
and the numbers will be picked with replacement, i.e., duplicates are allowed. The optional base
parameter is used to indicate that the client wishes the numbers to be returned in hexadecimal form. The numbers could be used as seed material for a pseudo-random number generator.
{ "jsonrpc": "2.0", "method": "generateIntegers", "params": { "apiKey": "6b1e65b9-4186-45c2-8981-b77a9842c4f0", "n": 512, "min": 0, "max": 255, "base": 16 }, "id": 4352 }
The service responds with the following:
{ "jsonrpc": "2.0", "result": { "random": { "data": [ "90", "a6", "3e", "f7", "06", ... ], "completionTime": "2011-10-10 13:19:12Z" }, "bitsUsed": 4096, "bitsLeft": 195904, "requestsLeft": 9999, "advisoryDelay": 0 }, "id": 4352 }
The random
object contains the random data generated by the server. For brevity, only the first five bytes are shown in the response. Note that the data
array contains strings rather than integers, because the numbers are formatted in base 16.
The service also advises how many true random bits were used to satisfy the request and how many bits and requests are left in the client's quota.