Skip to main content

Generate random numbers

Random number generation is crucial for ensuring fairness in decentralized applications, such as in games, gambling and other services, where unpredictable outcomes are required.

The following code allows generating a random number based on the last_block_time, block_height, op_index and seed.

function random(high: integer, seed: integer): integer {
if (high == 0) return 0;
return (op_context.last_block_time - op_context.block_height - op_context.op_index + seed) % high;
}

For a complete code example, refer to the Cookbook repository