0

I am using Oraclize for randomness. The range of random numbers I want depends on a variable maxNumber, and will be 0 - maxNumber.

If you scroll to the right of my code snippet below, you'll see maxNumber which I intuitively attempted to concat using +.

Off course that doesn't work and I get a compiler error since I cannot concat uint to a bytes32.

What is the proper way to concat in this situation?

bytes32 queryId = oraclize_query(
            "nested", 
            "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random[\"data\"]', '\\n{\"jsonrpc\": \"2.0\", \"method\": \"generateIntegers\", \"params\": { \"apiKey\": \"keyGoesHere\"}\", \"n\": 1,\"min\": 0, \"max\":'"+maxNumber-1+"', \"replacement\": true }, \"id\": 14215${[identity] \"}\"}']",
            gasForOraclize
        );
0

Use the uint2string method to convert the variable which holds a number (maxNumber) to a string. If you want to make the max range = maxNumber - 1 decrement maxNumber before the oraclize query as shown below.

uint maxNumber -= 1;

oraclize_query(
        "nested", 
        "[URL] ['json(https://api.random.org/json-rpc/1/invoke).result.random[\"data\"]', '\\n{\"jsonrpc\": \"2.0\", \"method\": \"generateIntegers\", \"params\": { \"apiKey\": \"keyGoesHere\"}\", \"n\": 1,\"min\": 0, \"max\": uint2str(maxNumber), \"replacement\": true }, \"id\": 14215${[identity] \"}\"}']",
        gasForOraclize
    );
  • Thanks. However, that does not work. In your example, the variable name is inside of a string literal. ... I would need to concatenate the value of that variable some how, no? – Ryan Miller Apr 28 '18 at 19:07
  • Oh ok, I believe this answer may help you then ethereum.stackexchange.com/a/28671/33496 , in that example they use strConcat, and make the oraclize query 3 different strings which they then concatenate – savard Apr 28 '18 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.