智能合约生成随机数

1. oraclize官方提供的方法

https://github.com/oraclize/ethereum-examples/blob/master/solidity/random-datasource/randomExample.sol

/*
   Oraclize random-datasource example

   This contract uses the random-datasource to securely generate off-chain N random bytes
*/

pragma solidity ^0.4.11;

import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";

contract RandomExample is usingOraclize {
    
    event newRandomNumber_bytes(bytes);
    event newRandomNumber_uint(uint);

    function RandomExample() {
        oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof in the constructor
        update(); // let's ask for N random bytes immediately when the contract is created!
    }
    
    // the callback function is called by Oraclize when the result is ready
    // the oraclize_randomDS_proofVerify modifier prevents an invalid proof to execute this function code:
    // the proof validity is fully verified on-chain
    function __callback(bytes32 _queryId, string _result, bytes _proof)
    { 
        if (msg.sender != oraclize_cbAddress()) throw;
        
        if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
            // the proof verification has failed, do we need to take any action here? (depends on the use case)
        } else {
            // the proof verification has passed
            // now that we know that the random number was safely generated, let's use it..
            
            newRandomNumber_bytes(bytes(_result)); // this is the resulting random number (bytes)
            
            // for simplicity of use, let's also convert the random bytes to uint if we need
            uint maxRange = 2**(8* 7); // this is the highest uint we want to get. It should never be greater than 2^(8*N), where N is the number of random bytes we had asked the datasource to return
            uint randomNumber = uint(sha3(_result)) % maxRange; // this is an efficient way to get the uint out in the [0, maxRange] range
            
            newRandomNumber_uint(randomNumber); // this is the resulting random number (uint)
        }
    }
    
    function update() payable {
        uint N = 7; // number of random bytes we want the datasource to return
        uint delay = 0; // number of seconds to wait before the execution takes place
        uint callbackGas = 200000; // amount of gas we want Oraclize to set for the callback function
        bytes32 queryId = oraclize_newRandomDSQuery(delay, N, callbackGas); // this function internally generates the correct oraclize_query and returns its queryId
    }
    
}

可靠性证明白皮书:

http://www.oraclize.it/papers/random_datasource-rev1.pdf

 

2. 通过random.org生成随机数,具官网介绍随机数生成是靠监控大气扰动随机得出的,请求格式大概如此,可参照etheroll代码

$.ajax({
            url: 'https://api.random.org/json-rpc/1/invoke',
            type:"POST",
            data:{
                'jsonrpc': '2.0',
                'method': 'generateIntegers',
                'params': {
                    'apiKey': '00000000-0000-0000-0000-000000000000',
                    'n': 10,
                    'min': 1,
                    'max': 10,
                    'replacement': true,
                    'base': 10
                },
                'id': 2601
            },
            contentType:"application/json; charset=utf-8",
            dataType:"json",
            success: function(result){
                $('#text').html(JSON.stringify(result));
                console.log(result);
                }
            }); 

 

posted @ 2018-04-19 16:39  huahuayu  阅读(2342)  评论(0编辑  收藏  举报