TensorFlow
Random Integers
Machine Learning
Python
Data Science

Generating random integers in TensorFlow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow, random integers are usually generated with tf.random.uniform by choosing an integer dtype such as tf.int32 or tf.int64. Even though the function name says uniform, it can produce either floating-point or integer tensors depending on the dtype you request.

The key details are the output shape, the inclusive lower bound minval, the exclusive upper bound maxval, and whether you need reproducible randomness. Once those are clear, integer generation is straightforward.

Generate Integer Tensors with tf.random.uniform

Here is the standard pattern:

python
1import tensorflow as tf
2
3x = tf.random.uniform(
4    shape=[5],
5    minval=0,
6    maxval=10,
7    dtype=tf.int32
8)
9
10print(x)

This creates a one-dimensional tensor of five random integers in the range 0 through 9. The lower bound is inclusive, and the upper bound is exclusive.

The same API works for matrices or higher-dimensional tensors:

python
1x = tf.random.uniform(
2    shape=[2, 3],
3    minval=1,
4    maxval=7,
5    dtype=tf.int32
6)
7print(x)

That produces a 2 x 3 tensor of integers from 1 through 6.

Control Reproducibility with a Seed

If you need reproducible results, set a TensorFlow seed:

python
1import tensorflow as tf
2
3tf.random.set_seed(1234)
4
5x1 = tf.random.uniform([4], minval=0, maxval=100, dtype=tf.int32)
6x2 = tf.random.uniform([4], minval=0, maxval=100, dtype=tf.int32)
7
8print(x1)
9print(x2)

With a fixed seed, the sequence is reproducible for the same program structure. That is useful in tests, debugging, and experiment setup.

Use Stateless Randomness When You Need Explicit Determinism

For stronger control, especially in functional or distributed code, use stateless random generation:

python
1import tensorflow as tf
2
3seed = [123, 456]
4x = tf.random.stateless_uniform(
5    shape=[5],
6    seed=seed,
7    minval=0,
8    maxval=10,
9    dtype=tf.int32
10)
11
12print(x)

Stateless generation depends only on the provided seed and arguments, not on global RNG state. That makes it easier to reason about in pipelines where reproducibility matters a lot.

Know the Bounds and Dtypes

There are two details that often trip people up:

  • 'maxval is exclusive'
  • for integer output, you must supply an integer dtype

So if you want random integers from 1 to 6, use minval=1 and maxval=7.

Also note that TensorFlow integer random generation is uniform over the integer range you specify. If you need a different discrete distribution, you must build that separately instead of expecting tf.random.uniform to do it automatically. That distinction matters in simulations and data sampling code where the probability model is part of the algorithm, not just a utility detail.

Use the Right Shape for the Job

The shape argument determines the layout of the resulting tensor, not the randomness itself. That means a batch of class IDs, random crop offsets, or synthetic labels can all be created with the same function as long as you choose the correct shape.

Examples:

python
batch_labels = tf.random.uniform([32], minval=0, maxval=5, dtype=tf.int32)
pixel_offsets = tf.random.uniform([8, 2], minval=0, maxval=16, dtype=tf.int32)

The first creates 32 random class IDs. The second creates eight pairs of random offsets.

Common Pitfalls

  • Forgetting that maxval is exclusive and wondering why the largest value never appears.
  • Using a floating-point dtype when you actually wanted integers.
  • Assuming a global seed gives perfect determinism in every complex training pipeline.
  • Using stateful randomness when stateless generation would make reproducibility easier to reason about.

Summary

  • Use tf.random.uniform(..., dtype=tf.int32) or tf.int64 to generate random integers.
  • 'minval is inclusive and maxval is exclusive.'
  • Set tf.random.set_seed for reproducible stateful generation.
  • Use tf.random.stateless_uniform when explicit deterministic control is important.
  • Choose the output shape to match the batch, matrix, or tensor structure you need.

Course illustration
Course illustration

All Rights Reserved.