rand(), rand_like(), randn(), randn_like(), randint() and randperm() in PyTorch
Tags: pytorch, rand, randn, randint
Published: 2024-04-08 14:12:05

*[My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html), [initial_seed()](https://pytorch.org/docs/stable/generated/torch.initial_seed.html) and [seed()](https://pytorch.org/docs/stable/generated/torch.seed.html).

[rand()](https://pytorch.org/docs/stable/generated/torch.rand.html) can create the 1D or more D tensor of zero or more random floating-point numbers(Default) or complex numbers between `0` and `1` as shown below:

*Memos:
- `rand()` can be used only from [torch](https://pytorch.org/docs/stable/torch.html) but not from a tensor.
- The default type is `float32`.
- `0` is included but `1` is not included.

```python
import torch

torch.rand(0)
torch.rand((0,))
# tensor([])

torch.rand(3)
torch.rand((3,))
# tensor([0.9544, 0.8922, 0.3293])

torch.rand(3, 2)
torch.rand((3, 2))
# tensor([[0.4453, 0.1646], [0.0770, 0.6157], [0.3791, 0.9814]])

torch.rand(3, 2, 4)
torch.rand((3, 2, 4))
# tensor([[[0.6002, 0.9127, 0.1574, 0.0520],
#          [0.6030, 0.8130, 0.8485, 0.0871]],
#         [[0.7439, 0.3891, 0.0515, 0.9340],
#          [0.8608, 0.5153, 0.5754, 0.7751]],
#         [[0.9068, 0.6578, 0.6610, 0.5676],
#          [0.3567, 0.2954, 0.5270, 0.8195]]])

torch.rand(3, 2, 4, dtype=torch.complex64)
torch.rand((3, 2, 4), dtype=torch.complex64)
# tensor([[[0.5424+0.6970j, 0.7606+0.3126j,
#           0.9649+0.6356j, 0.3805+0.3811j],
#          [0.7923+0.3020j, 0.9168+0.1565j,
#           0.9722+0.7607j, 0.2757+0.8831j]],
#         [[0.7804+0.0009j, 0.4435+0.9249j, 
#           0.2160+0.5390j, 0.9973+0.4470j],
#          [0.2328+0.5351j, 0.0051+0.3250j,
#           0.5077+0.4365j, 0.4540+0.2164j]],
#         [[0.7434+0.1432j, 0.0507+0.7610j,
#           0.0449+0.6918j, 0.8486+0.5216j],
#          [0.2609+0.1117j, 0.3226+0.3598j,
#           0.9357+0.8471j, 0.1643+0.6220j]]])
```

[rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html) can replace the zero or more floating-point numbers of 0D or more D tensor with zero or more random floating-point numbers or complex numbers between `0` and `1` as shown below:

*Memos:
- `rand_like()` can be used only from `torch` but not from a tensor.
- `0` is included but `1` is not included.

```python
import torch

my_tensor = torch.tensor(7.)
torch.rand_like(my_tensor)
# tensor(0.3634)

my_tensor = torch.tensor([7., 4., 5.])
torch.rand_like(my_tensor)
# tensor([0.9000, 0.1314, 0.5240])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])
torch.rand_like(my_tensor)
# tensor([[0.7508, 0.5857, 0.1658],
#         [0.3327, 0.1836, 0.3776]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]], 
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.rand_like(my_tensor)
# tensor([[[0.8467, 0.3134, 0.9486], [0.3134, 0.1809, 0.1740]],
#         [[0.2624, 0.3929, 0.2138], [0.2034, 0.9147, 0.3421]]])

my_tensor = torch.tensor([[[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]],
                          [[6+9j, 0+3j, 1+8j], [5+3j, 9+4j, 4+6j]]])
torch.rand_like(my_tensor)
# tensor([[[0.9179+0.7615j, 0.6811+0.3994j, 0.6854+0.3131j],
#          [0.7352+0.0108j, 0.0999+0.4509j, 0.9586+0.1530j]],
#         [[0.5253+0.7685j, 0.5731+0.0358j, 0.9499+0.8943j],
#          [0.7742+0.3421j, 0.0419+0.8636j, 0.1014+0.8507j]]])
```

[randn()](https://pytorch.org/docs/stable/generated/torch.randn.html) can create the 1D or more D tensor of zero or more random floating-point numbers(Default) or complex numbers most of the time about between `2` and `-2` from standard normal distribution as shown below:

*Memos:
- `randn()` can be used only from `torch` but not from a tensor.
- The default type is `float32`.

```python
import torch

torch.randn(0)
torch.randn((0,))
# tensor([])

torch.randn(3)
torch.randn((3,))
# tensor([-0.6635,  1.6257, -2.0568])

torch.randn(3, 2)
torch.randn((3, 2))
# tensor([[-1.9382, 1.1300], [0.4138, 0.8232], [-0.9810, -0.1410]])

torch.randn(3, 2, 4)
torch.randn((3, 2, 4))
# tensor([[[-0.1585, 0.6327, -0.2487, -0.0963],
#          [-0.2869,  2.4429,  0.5098, -1.1569]],
#         [[ 0.2823,  1.0742,  1.0531, -0.2384],
#          [-0.2275, -2.6246,  0.3347, -1.6514]],
#         [[ 0.2934, -1.2568,  0.1798,  1.1989],
#          [ 0.5166, -1.2155,  0.5101, -0.7396]]])

torch.randn(3, 2, 4, dtype=torch.complex64)
torch.randn((3, 2, 4), dtype=torch.complex64)
# tensor([[[0.5911+0.4230j, -0.1695-0.5487j,
#           -0.5253-1.3477j, 0.2331+1.9928j],
#          [1.1815+0.8113j, 0.1307+2.0229j,
#           0.5588+0.6502j, 0.8352-1.1519j]],
#         [[1.5789+0.5048j, -1.2516-1.0842j,
#           0.6233+0.7139j, 0.0393-0.4259j],
#          [0.1236+0.9666j, 0.2755+0.1572j,
#           -0.5766-0.0394j, -0.3715-0.2530j]],
#         [[0.6339-1.1397j, -0.1932-0.8051j,
#           1.3219-0.4141j, 0.0786+0.1172j],
#          [-0.9245+0.9382j, -1.0984+0.4884j,
#           -0.3895-0.0102j, -0.0171+0.9639j]]])
```

[randn_like()](https://pytorch.org/docs/stable/generated/torch.randn_like.html) can replace the zero or more floating-point numbers or complex numbers of 0D or more D tensor with zero or more random floating-point numbers most of the time about between `2` and `-2` from standard normal distribution as shown below. *`randn_like()` can be used only from `torch` but not from a tensor:

```python
import torch

my_tensor = torch.tensor(7.)
torch.randn_like(my_tensor)
# tensor(-2.3177)

my_tensor = torch.tensor([7., 4., 5.])
torch.randn_like(my_tensor)
# tensor([-0.4706, -0.0940, 1.7397])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])
torch.randn_like(my_tensor)
# tensor([[-0.4152, 0.8599, -2.4599],
#         [-1.1088, -0.4828, 2.3003]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.randn_like(my_tensor)
# tensor([[[-1.0316, 1.5852, -1.1012], [-2.3114, 0.5584, 2.3333]],
#         [[-0.5730, -0.3688, 0.0671], [1.2493, -0.0072, 0.1905]]])

my_tensor = torch.tensor([[[7+4j, 4+2j, 5+3j], [2+5j, 8+1j, 3+9j]],
                          [[6+9j, 0+3j, 1+8j], [5+3j, 9+4j, 4+6j]]])
torch.randn_like(my_tensor)
# tensor([[[-0.3193-0.2821j, -0.8942+0.7886j, 0.2968-0.0608j],
#         [-0.5996+0.8685j, 0.7314-0.5378j, 1.7339-0.8352j]],
#        [[-0.0787-0.1357j, -1.4519-0.0295j, 0.1233-0.1794j],
#         [ 0.4109-0.2422j, 0.3973+0.3363j, -1.0303-1.4719j]]])
```

[randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) create the 1D or more D tensor of zero or more random integers(Default) or floating-point numbers between two numbers which you set as shown below:

*Memos:
- `randint()` can be used only from `torch` but not from a tensor.
- Setting only two arguments sets `0` as the start number.
- The default type is `int64`.

```python
import torch

torch.randint(10, (3,))
# tensor([7, 4, 8])

torch.randint(10, (3, 2))
# tensor([[8, 9], [6, 5], [5, 2]])

torch.randint(10, (3, 2, 4))
# tensor([[[1, 5, 9, 0], [4, 6, 7, 2]],
#         [[5, 2, 1, 5], [9, 3, 2, 6]],
#         [[9, 3, 6, 4], [0, 4, 7, 5]]])

torch.randint(10, (3, 2, 4), dtype=torch.float32)
# tensor([[[3., 4., 1., 9.], [0., 1., 7., 0.]],
#         [[7., 3., 9., 6.], [2., 3., 2., 5.]],
#         [[8., 8., 4., 2.], [8., 5., 2., 4.]]])

torch.randint(10, 20, (3,))
# tensor([17, 12, 10])

torch.randint(10, 20, (3, 2))
# tensor([[14, 18], [10, 19], [15, 16]])

torch.randint(10, 20, (3, 2, 4))
# tensor([[[16, 14, 11, 19], [19, 15, 18, 13]],
#         [[14, 10, 11, 13], [16, 11, 10, 16]], 
#         [[17, 12, 17, 10], [13, 16, 11, 10]]])

torch.randint(10, 20, (3, 2, 4), dtype=torch.float32)
# tensor([[[18., 10., 12., 13.], [14., 15., 15., 12.]],
#         [[19., 10., 17., 14.], [16., 14., 17., 10.]],
#         [[14., 12., 16., 10.], [13., 19., 18., 11.]]])

torch.randint(-5, 5, (3,))
# tensor([-1,  2, -3])

torch.randint(-5, 5, (3, 2))
# tensor([[-5,  4], [ 1, -1], [-4, -3]])

torch.randint(-5, 5, (3, 2, 4))
# tensor([[[-2, 0, 1, -5], [4, -5, -3, 1]],
#         [[-4, -1, -1, -1], [-3, 2, -4, -1]],
#         [[4, -1, -5, -3], [2, -3, -2, 2]]])

torch.randint(-5, 5, (3, 2, 4), dtype=torch.float32)
# tensor([[[-4., 1., -1., -3.], [-3., -5., -4., 1.]],
#         [[-5., 3., 3., 1.], [-1., 4., -5., 2.]],
#         [[-2., -4., -5., 3.], [4., 1., -3., 3.]]])

torch.randint(1, (0,))
torch.randint(0, 1, (0,))
torch.randint(10, 20, (0,))
# tensor([], dtype=torch.int64)
```

[randperm()](https://pytorch.org/docs/stable/generated/torch.randperm.html) can create the 1D tensor of zero or more random integers(Default) or floating-point numbers between `0` and the number which you set as the 1st argument as shown below:

*Memos:
- `randperm()` can be used only from `torch` but not from a tensor.
- The default type is `int64`.

```python
import torch

torch.randperm(0)
# tensor([], dtype=torch.int64)

torch.randperm(5)
# tensor([3, 0, 4, 2, 1])

torch.randperm(10)
# tensor([8, 6, 9, 2, 1, 3, 5, 0, 7, 4])

torch.randperm(10, dtype=torch.float32)
# tensor([7., 4., 2., 1., 8., 3., 0., 6., 9., 5.])
```