# Using torch.randint() and torch.randint_like() to create Random Tensors in PyTorch

## Introduction

In this article, we will show you how to create random tensors in PyTorch with torch.randint() and torch.randint_like() functions. These functions are very useful to initialize neural networks with random values at the start of the training process. We shall cover these functions with the help of examples for easy understanding for beginners.

## Random PyTorch Tensors with torch.randint()

torch.randint function is used to create a tensor with random integer values between the low (inclusive) and high (exclusive) value that is specified in the function.

To go through the examples of torch randint function let us first import the PyTorch library.

In [0]:

`import torch;`

### Example – 1: Creating 1 Dimensional Random Tensor with torch.randint()

In the first example, we are going to create a 1D tensor of 6 elements with random integers between 0 and 20. Do notice that in this example we have not specified low parameter, as a result, it is assumed to be 0 as default.

In [1]:

```tensor = torch.randint(high = 20, size = (6,))

tensor```

Out[1]:

`tensor([ 5, 18, 10,  5, 17, 12])`

### Example – 2: Creating 1 Dimensional Random Tensor with Low and High

In this example, we are specifying both values of low and high to create a random tensor of size 6.

In [2]:

```tensor = torch.randint(low = 10, high = 20, size = (6,))

tensor```

Out[2]:

`tensor([17, 13, 11, 17, 11, 15])`

### Example – 3: Creating 2 Dimensional Random Tensor

Here we are creating 2D random tensor of size 4×5 with torch randint function.

In [3]:

```tensor = torch.randint(low = 10, high = 50, size = (4,5))

tensor```

Out[3]:

```tensor([[44, 38, 39, 32, 34],
[18, 29, 36, 23, 17],
[13, 10, 10, 14, 32],
[44, 39, 45, 21, 48]])```
Out[4]:
```tensor([[[14, 42, 13, 35, 14],
[34, 24, 44, 49, 39],
[24, 11, 29, 46, 43],
[11, 15, 15, 46, 49]],

[[36, 48, 13, 26, 20],
[16, 35, 20, 47, 16],
[39, 44, 15, 25, 23],
[42, 18, 29, 12, 34]]])```

### Example – 5: Using dtype parameter with torch.randint

By default torch randint creates a tensor whose elements are of type int. However, you can use dtype parameter to specify other data types. In this example, we are using dtype parameter as float.

In [5]:

```tensor = torch.randint(low = 10, high = 50, size = (4,5), dtype = float)

tensor```
Out[5]:
```tensor([[27., 11., 42., 33., 48.],
[43., 43., 11., 27., 17.],
[31., 30., 39., 15., 38.],
[10., 20., 18., 29., 35.]], dtype=torch.float64)```

### Example – 6: Using Seed Value with torch.randint

torch randint function will generate different random values of tensor whenever it is executed. However, we can use a seed value to make sure it generates the same value whenever it is executed.

To start with, we create a generator object by initializing with a seed number with manual_seed. Next, this generator object is used inside torch.randint function that will make sure the same values are generated in the tensor whenever the code is executed.

In[6]:

```g_cpu = torch.Generator()

g_cpu.manual_seed(2542587391)

tensor = torch.randint(low = 25, high = 125, size = (4,3), generator = g_cpu)

tensor```
Out[6]:
```tensor([[ 29,  64,  30],
[ 92,  51, 122],
[ 45, 122,  35],
[ 83, 123, 107]])```

## Random PyTorch Tensors with torch.randint_like()

There might be a situation where you may have to create a random tensor whose size is same as another tensor. To achieve this you can manually get the size of the other tensor and then create the random tensor using that. Alternatively, there is a more convenient way of using torch.randint_like() function to achieve the same.

To understand this function let us see some examples.

### Example – 1: Creating 1-D Random Tensor with torch.randint_like()

Let us first create a zero tensor of size 5 and then use it inside torch.randint_like function as a reference for size along with low & high parameters.

It is interesting to note that by default zero tensor is of float type and the same data type is inherited by the random tensor too.

In [7]:

```input_tensor = torch.zeros(size=(5,))

input_tensor```
Out[7]:
`tensor([0., 0., 0., 0., 0.])`
In [8]:
```tensor = torch.randint_like(input = input_tensor,low = 10, high = 50)

tensor```
Out[8]:
`tensor([12., 10., 10., 45., 17.])`

### Example – 2: Creating 2-D Random Tensor with torch.randint_like()

In this example, we create a 5×3 2-D zeros tensor by specifying the data type as int. Then we use this with torch randint_like function to create random tensor of same size.
This time since the zero tensor is of type int, the generated random tensor is also of type int.

In [9]:

```input_tensor = torch.zeros(size=(5,3), dtype = int)

input_tensor```
Out[9]:
```tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])```
In [10]:
```tensor = torch.randint_like(input = input_tensor,low = 10, high = 50)

tensor```
Out[10]:
```tensor([[42, 13, 49],
[32, 35, 42],
[19, 23, 28],
[22, 16, 39],
[25, 10, 20]])```

### Example – 3: Creating 3-D Random Tensor with torch.randint_like()

Here we are creating a 3D zeros tensor of size 2x5x3 and then use it to create an integer random tensor of the same size with torch rand int function.

In [11]:

```input_tensor = torch.zeros(size=(2,5,3))

input_tensor```
Out[11]:
```tensor([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],

[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]])```
In [12]:
```tensor = torch.randint_like(input = input_tensor,low = 10, high = 50, dtype=int)

tensor```
Out[12]:
```tensor([[[35, 35, 10],
[12, 13, 23],
[45, 22, 34],
[31, 46, 12],
[25, 18, 10]],

[[41, 44, 42],
[32, 20, 46],
[15, 19, 14],
[16, 34, 13],
[32, 39, 11]]])```

• MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.