# Random integers

**URL:** <https://discuss.pytorch.org/t/random-integers/15509>\
**Category:** Uncategorized\
**Created:** [March 26, 2018, 5:53pm UTC](https://discuss.pytorch.org/t/random-integers/15509 "2018-03-26T17:53:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanonardo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stefanonardo/32/2322_2.png) [@stefanonardo](https://discuss.pytorch.org/u/stefanonardo)\
**Post date:** [March 26, 2018, 5:53pm UTC](https://discuss.pytorch.org/t/random-integers/15509/1 "2018-03-26T17:53:20Z")

</div>

Is there anything similar to NumPy `random.randint` to generate random integers between a certain range?

---

<div class="post-metadata">

**Author:** ![adrianjav](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adrianjav/32/12404_2.png) [@adrianjav](https://discuss.pytorch.org/u/adrianjav)\
**Post date:** [March 26, 2018, 6:38pm UTC](https://discuss.pytorch.org/t/random-integers/15509/2 "2018-03-26T18:38:28Z")

</div>

[`torch.rand`](http://pytorch.org/docs/0.3.1/torch.html?highlight=torch%20random#torch.rand) outputs a tensor fill out with random numbers within `[0,1)`. You can use that and convert it to the range `[l,r)` using a formula like `l + torch.rand() * (r - l)` and then converting them to integers as usual.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 26, 2018, 9:04pm UTC](https://discuss.pytorch.org/t/random-integers/15509/3 "2018-03-26T21:04:30Z")

</div>

You could also directly set the range using:

```
torch.LongTensor(10).random_(0, 10)

```

where numbers define the lower and upper bound, respectively.

---

<div class="post-metadata">

**Author:** ![Adam\_Dziedzic](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adam_dziedzic/32/5013_2.png) [@Adam\_Dziedzic](https://discuss.pytorch.org/u/Adam_Dziedzic)\
**Post date:** [July 2, 2018, 12:39pm UTC](https://discuss.pytorch.org/t/random-integers/15509/4 "2018-07-02T12:39:18Z")

</div>

PyTorch also supports randint: [https://pytorch.org/docs/stable/torch.html#torch.randint](https://pytorch.org/docs/stable/torch.html#torch.randint)

```auto
torch.randint(3, 5, (3,))
tensor([4., 3., 4.])

```

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [August 5, 2020, 5:39pm UTC](https://discuss.pytorch.org/t/random-integers/15509/5 "2020-08-05T17:39:11Z")

</div>

this should work:

```auto
import torch

batch_size = 16
y = torch.randint(low=0,high=Dout,size=(batch_size,))

```

docs: [https://pytorch.org/docs/stable/generated/torch.randint.html#torch.randint](https://pytorch.org/docs/stable/generated/torch.randint.html#torch.randint)
