Saturday, November 15, 2025
HomeLanguagesJavascriptTensorflow.js tf.randomUniform() Function

Tensorflow.js tf.randomUniform() Function

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.

The tf.randomUniform() function is used to create a tf.Tensor with values sampled from a uniform distribution.

Syntax:

tf.randomUniform (shape, minval, maxval, dtype, seed)

Parameter: This function accepts five parameters which are illustrated below:

  • shape: An array of integers defining the shape of the output tensor.
  • minval: It is an optional argument. The lower bound of the uniform distribution range. The default value is 0.
  • maxval: It is also an optional argument. It is the upper bound of the steady distribution range. It is not included in the range. The default value is 1.
  • dtype: The data type of the output. The values of datatype possible are ‘float32’ , ‘int32’, ‘ ‘bool’ , ‘complex64’ , ‘string’. It is also an optional argument. The default value is ‘float32’
  • seed: It is an optional argument. The seed for the random number generator.

Return: It returns tf.Tensor.

Example 1:

Javascript




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a uniform distribution
const x=tf.randomUniform([5]);
  
// Printing the tensor
x.print();


Output:

Tensor
    [0.0008758, 0.3491586, 0.3466536, 0.9614096, 0.7892056]

Example 2:

Javascript




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a normal distribution
const x=tf.randomUniform([2, 2]);
  
// Printing the tensor
x.print();


Output:

Tensor
    [[0.7312108, 0.5003704],
    [0.8552292, 0.082417 ]]

Example 3:

Javascript




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a normal distribution
const x=tf.randomUniform([5], 10, 15, 'int32', 0);
  
// Printing the tensor
x.print();


Output:

Tensor
    [12, 14, 10, 13, 12]

Reference: https://js.tensorflow.org/api/latest/#randomUniform

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6890 POSTS0 COMMENTS
Ted Musemwa
7143 POSTS0 COMMENTS
Thapelo Manthata
6838 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS