Im using Oraclize to get a random number but the N of bytes confuses me

It seems like when I give the Oraclize query a N of 4, it always returns a 19 digit number, this might be a coincidence but I think not. What if I actually want a number between lets say 0 and 10 * 10^18?

Is this possible? This is how how my Oraclize looks now:

``````    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);
``````

And in the __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
``````

It looks like you're using the random bytes to generate a number between `1` and `maxRange`, inclusive. If you want a number between `0` and `10**18`, set `maxRange` to `10**18` and drop the `+ 1` in your code.

But note that 4 random bytes is not enough entropy to generate numbers in that range. 4 bytes gives you only `2**32` different values, which is `4,294,967,296`. This is much smaller than `10**18`. You'll need to use 8 bytes. (`log2(10^18) ~= 60 bits`.)

• Thanks for your answer, and yes the 0 to 10^18 was a mistake It's meant to be 1 to 10^18, so an outcome of this random number could be for example 67? Ive only seen really high numbers but the testing is so slow that it could be a coincidence. – jasper Jun 1 '18 at 17:28
• Yes, but a number that small would be extraordinarily unlikely. If you picked numbers between 0 and 999, 90% of them would be three digits. (100/1000 are two digits or fewer.) – user19510 Jun 1 '18 at 18:58