randint() and randperm() in PyTorch
Published: 2024-07-10 14:29:45

*Memos:
- You can use [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html) with [randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) and [randperm()](https://pytorch.org/docs/stable/generated/torch.randperm.html). *[My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains `manual_seed()`.
- [My post](https://dev.to/hyperkai/rand-and-randlike-in-pytorch-1p3k) explains [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html) and [rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html).
- [My post](https://dev.to/hyperkai/randn-and-randnlike-in-pytorch-8bb) explains [randn()](https://pytorch.org/docs/stable/generated/torch.randn.html) and [randn_like()](https://pytorch.org/docs/stable/generated/torch.randn_like.html).
- [My post](https://dev.to/hyperkai/normal-in-pytorch-2jb) explains [normal()](https://pytorch.org/docs/stable/generated/torch.normal.html).

[randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) can create the 1D or more D tensor of the zero or more random integers(Default) or floating-point numbers between `low` and `high-1`(`low`<=x<=`high-1`) as shown below:

*Memos:
- `randint()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument with `torch` is `low`(Optional-Default:`0`-Type:`int`): 
  *Memos:
  - It must be lower than `high`.
  - The 0D or more D tensor of one integer works.
- The 2nd argument with `torch` is `high`(Required-Type:`int`):
  *Memos:
  - It must be greater than `low`.
  - The 0D or more D tensor of one integer works.
- The 3rd argument with `torch` is `size`(Required-Type:`tuple` of `int` or `list` of `int`).
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is `torch.int64`.
  - `int` or `float` can be used.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

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

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

torch.randint(high=10, size=(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(low=10, high=20, size=(3,))
# tensor([17, 12, 10])

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

torch.randint(low=10, high=20, size=(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(low=-5, high=5, size=(3,))
# tensor([-1,  2, -3])

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

torch.randint(low=-5, high=5, size=(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(low=-5, high=5, size=(3, 2, 4), dtype=torch.float32)
torch.randint(low=torch.tensor(-5),
              high=torch.tensor([5]),
              size=(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(high=1, size=(0,))
torch.randint(low=0, high=1, size=(0,))
torch.randint(low=10, high=20, size=(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 `n-1`(0<=x<=`n-1`) as shown below:

*Memos:
- `randperm()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `n`(Required-Type:`int`): 
  *Memos:
  - It must be greater than or equal to 1.
  - The 0D or more D tensor of one integer works.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is `torch.int64`.
  - `int` or `float` can be used.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

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

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

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

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