Skip to content

Random Number

The random_number resource generates random integers within a specified range. It provides deterministic random number generation that remains consistent across multiple runs.

As a lab author, you can use random_number resources to:

  • Port Assignment: Generate random port numbers for services to avoid conflicts in multi-instance lab environments
  • Load Balancing: Create random weights or priorities for load balancing scenarios and traffic distribution
  • Network Configuration: Create random VLAN IDs, network segments, or routing priorities for network labs

Random number resources provide controlled randomness for lab configuration while ensuring reproducible results.

resource "random_number" "name" {
minimum = 1000
maximum = 2000
}
resource "random_number" "name" {
minimum = 10000
maximum = 65535
}
FieldRequiredTypeDescription
minimumintThe minimum number to generate (inclusive)
maximumintThe maximum number to generate (inclusive)

These attributes are set by the system after number generation:

Field Type Description
value int The generated random number
  • Minimum value must be less than or equal to maximum value
  • Both minimum and maximum must be valid integers
  • Generated values remain constant across multiple runs (idempotent)
  • Random values are deterministically generated based on resource configuration
resource "random_number" "port" {
minimum = 10000
maximum = 20000
}
resource "container" "web" {
image {
name = "nginx:alpine"
}
port {
local = resource.random_number.port.value # e.g., 15847
host = resource.random_number.port.value # e.g., 15847
}
}
output "assigned_port" {
value = resource.random_number.port.value # e.g., 15847
}
resource "random_number" "weight" {
minimum = 1
maximum = 100
}
resource "template" "nginx_config" {
source = <<-EOF
upstream backend {
server backend1:8080 weight=${resource.random_number.weight.value}; # e.g., weight=73
server backend2:8080 weight=${100 - resource.random_number.weight.value}; # e.g., weight=27
}
EOF
destination = "./nginx.conf"
}
resource "random_number" "cpu_shares" {
minimum = 1
maximum = 4
}
resource "random_number" "memory_mb" {
minimum = 512
maximum = 2048
}
resource "container" "app" {
image {
name = "myapp:latest"
}
resources {
cpu = resource.random_number.cpu_shares.value * 250 # e.g., 750 (if cpu_shares.value = 3)
memory = resource.random_number.memory_mb.value # e.g., 1536 (if memory_mb.value = 1536)
}
}
resource "random_number" "subnet_octet" {
minimum = 1
maximum = 254
}
resource "network" "lab_network" {
subnet = "10.${resource.random_number.subnet_octet.value}.0.0/24" # e.g., "10.142.0.0/24"
}
# Use random number in template for network configuration
resource "template" "network_config" {
source = <<-EOF
network:
subnet: 10.${resource.random_number.subnet_octet.value}.0.0/24
vlan_id: ${resource.random_number.subnet_octet.value + 100}
EOF
destination = "./network.yaml"
}
resource "random_number" "instance_count" {
minimum = 2
maximum = 8
}
resource "container" "worker" {
count = resource.random_number.instance_count.value # e.g., 5 containers created
image {
name = "worker:latest"
}
environment = {
WORKER_ID = tostring(count.index + 1) # e.g., "1", "2", "3", "4", "5"
TOTAL_WORKERS = tostring(resource.random_number.instance_count.value) # e.g., "5"
}
}
output "worker_count" {
value = resource.random_number.instance_count.value # e.g., 5
description = "Number of worker instances created"
}
  1. Range Selection: Choose appropriate minimum and maximum values for your use case
  2. Collision Avoidance: Use sufficiently wide ranges to minimize collision probability
  3. Resource Naming: Use descriptive names to clarify the random number’s purpose
  4. Validation: Ensure generated values work with target systems and configurations
  5. Documentation: Document the purpose and expected range of random values
  6. Determinism: Remember that values are consistent across runs for the same configuration
  7. Testing: Test with various generated values to ensure system robustness