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.
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();
}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();
}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.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 →