io.random()
io.random()
is identical to Math.random()
when called without options but ensures your random numbers are not regenerated on resume or retry. It will return a pseudo-random floating-point number between optional min
(default: 0, inclusive) and max
(default: 1, exclusive). Can optionally round
to the nearest integer.
client.defineJob({
id: "random-job",
name: "Random Job",
version: "0.0.1",
trigger: eventTrigger({
name: "example.event",
}),
run: async (payload, io, ctx) => {
// generate random numbers
const small = await io.random("random-small");
const large = await io.random("random-large", {
min: 10,
max: 200,
round: true
});
await io.logger.info(`${small} is smaller than ${large}`);
},
});
Parameters
Should be a stable and unique cache key inside the run()
. See
resumability for more information.
Sets the lower bound (inclusive). Can’t be higher than max
.
Sets the upper bound (exclusive). Can’t be lower than min
.
Controls rounding to the nearest integer. Any max
integer will become inclusive when enabled. Rounding with floating-point bounds may cause unexpected skew and boundary inclusivity.
Returns
A Promise
that resolves with a pseudo-random number. Always resolves to an integer when rounding is enabled.
Was this page helpful?
client.defineJob({
id: "random-job",
name: "Random Job",
version: "0.0.1",
trigger: eventTrigger({
name: "example.event",
}),
run: async (payload, io, ctx) => {
// generate random numbers
const small = await io.random("random-small");
const large = await io.random("random-large", {
min: 10,
max: 200,
round: true
});
await io.logger.info(`${small} is smaller than ${large}`);
},
});