Estoy usando Oraclize para obtener un número aleatorio pero el N de bytes me confunde

Parece que cuando le doy a la consulta de Oracle un N de 4, siempre devuelve un número de 19 dígitos, esto podría ser una coincidencia, pero creo que no. ¿Qué sucede si realmente quiero un número entre, digamos, 0 y 10 * 10 ^ 18?

es posible? Así es como se ve mi Oraclize ahora:

    oraclize_setProof(proofType_Ledger); // sets the Ledger authenticity proof
    uint N = 4; // 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);

Y en el __callBack

        uint maxRange = totalEth -1; // deduct one so that when one gets added later it cant be bigger than the total eth.
        randomNumber = (uint(sha3(_result)) % maxRange) + 1 // this is an efficient way to get the uint out in the [1, maxRange] range

Respuestas (1)

Parece que está utilizando los bytes aleatorios para generar un número entre 1y maxRange, inclusive. Si desea un número entre 0y 10**18, configúrelo maxRangey 10**18suéltelo + 1en su código.

Pero tenga en cuenta que 4 bytes aleatorios no son suficiente entropía para generar números en ese rango . 4 bytes le da solo 2**32valores diferentes, que es 4,294,967,296. Esto es mucho más pequeño que 10**18. Necesitarás usar 8 bytes . ( log2(10^18) ~= 60 bits.)

Gracias por su respuesta, y sí, el 0 a 10 ^ 18 fue un error. Se supone que debe ser 1 a 10 ^ 18, por lo que el resultado de este número aleatorio podría ser, por ejemplo, 67. Solo he visto números realmente altos, pero la prueba es tan lenta que podría ser una coincidencia.
Sí, pero un número tan pequeño sería extraordinariamente improbable. Si eligió números entre 0 y 999, el 90% de ellos serían tres dígitos. (100/1000 son dos dígitos o menos).