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:
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:
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:
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:
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:
- '
maxvalis 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:
The first creates 32 random class IDs. The second creates eight pairs of random offsets.
Common Pitfalls
- Forgetting that
maxvalis 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)ortf.int64to generate random integers. - '
minvalis inclusive andmaxvalis exclusive.' - Set
tf.random.set_seedfor reproducible stateful generation. - Use
tf.random.stateless_uniformwhen explicit deterministic control is important. - Choose the output shape to match the batch, matrix, or tensor structure you need.

