1. Answers
  2. Generating Random Integers

How do I generate random integers?

To generate a random integer, we will use the random_integer resource which allows us to create a random value. This can be especially useful for scenarios such as generating unique numeric IDs or for test data.

In the following example, we configure the provider and then declare a random_integer resource with specific minimum and maximum bounds. After the integer is generated, we output its value so it can be easily accessed.

The program is defined as follows:

import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";

const example = new random.RandomInteger("example", {
    min: 1,
    max: 100,
});
export const randomIntegerValue = example.result;

In this example:

  • The random provider is initialized.
  • A random integer between 1 and 100 is generated.
  • The result is outputted for use in other parts of your infrastructure configuration.

This concludes the example of generating a random integer. This example helps create random numbers for use cases where unique numeric values are needed.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up