Generate a Random Number in Zoho Creator with Deluge

Generate Random Numbers in Zoho Creator

Deluge has no random(). That is a surprise the first time you need a reference code, a raffle winner or a sampling key. There are two honest ways around it, and which one you want depends on whether anybody would benefit from predicting the result.

The RANDOM.ORG endpoint in the original is officially obsolete. RANDOM.ORG’s own documentation for the old HTTP interface now opens with: “This API is obsolete. Please use our new API at api.random.org instead.” The legacy URL still answers, but it is unsupported. The original also called /sequences/ — which shuffles a range rather than drawing from it — passed format twice with conflicting values, and used the legacy getUrl() task.

Option 1 — local, no network

Good enough for anything where nobody gains by guessing: a display order, a sampling bucket, a suffix on a reference number.

int API.randomNumber(int minVal, int maxVal)
{
    // Deluge has no built-in random. Time in milliseconds is the seed.
    span = maxVal - minVal + 1;
    millis = zoho.currenttime.toTime("dd-MMM-yyyy HH:mm:ss").getTime();
    return minVal + (millis % span).toLong();
}

Option 2 — true randomness from RANDOM.ORG

RANDOM.ORG derives its numbers from atmospheric noise rather than a formula. Worth it when the result has to be defensible — a prize draw, an audit sample, anything somebody might contest.

int API.trueRandomNumber(int minVal, int maxVal)
{
    API_KEY = "YOUR_RANDOM_ORG_API_KEY";

    params = Map();
    params.put("apiKey",API_KEY);
    params.put("n",1);
    params.put("min",minVal);
    params.put("max",maxVal);
    params.put("replacement",true);

    payload = Map();
    payload.put("jsonrpc","2.0");
    payload.put("method","generateIntegers");
    payload.put("params",params);
    payload.put("id",1);

    response = invokeurl
    [
        url    : "https://api.random.org/json-rpc/4/invoke"
        type   : POST
        parameters : payload.toString()
        headers: {"Content-Type":"application/json"}
    ];

    result = response.getJSON("result");
    if(result == null)
    {
        info "RANDOM.ORG error: " + response.toString();
        return minVal;                      // fail to a safe value, do not throw
    }
    return result.getJSON("random").getJSON("data").get(0).toLong();
}

Notes

  • The clock-based version is not secure. Two records created in the same millisecond get the same number, and anyone who knows roughly when a record was made can narrow the value down hard. Never use it for passwords, tokens, discount codes or anything a person would gain from guessing.
  • Quota is charged per IP, and you do not own the IP. RANDOM.ORG gives each address a base allowance of one million bits with a daily top-up. Your Deluge calls leave from Zoho’s shared outbound addresses, so you may find the quota already spent by somebody else entirely. This is the strongest practical argument for using an API key, which meters against your account instead.
  • Always have a fallback. The function above returns minVal rather than throwing when the service is unreachable. A raffle that errors is worse than a raffle that logs a problem and keeps going.
  • For unique IDs, do not use randomness at all. Creator’s auto-number field and the record ID are already unique and cost nothing. Random values need a collision check; sequences do not.

This script is part of the free Creator Scripts Deluge Library.

All 39 Deluge scripts, the full Zoho Creator course, and every downloadable asset are now free. Get free access →