# How to use RandomSampler

**URL:** <https://discuss.pytorch.org/t/how-to-use-randomsampler/68376>\
**Category:** Uncategorized\
**Created:** [February 1, 2020, 11:55pm UTC](https://discuss.pytorch.org/t/how-to-use-randomsampler/68376 "2020-02-01T23:55:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![klory](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/klory/32/5814_2.png) [@klory](https://discuss.pytorch.org/u/klory)\
**Post date:** [February 1, 2020, 11:55pm UTC](https://discuss.pytorch.org/t/how-to-use-randomsampler/68376/1 "2020-02-01T23:55:32Z")

</div>

Ubuntu 18.04 or Mac OS Catalina, Python 3.7, PyTorch 1.4.0

I try to sample from a dataset using predefined indices, `SubsetRandomSampler` works as expected, `RandomSampler` does not work as expected, I check the [source code](https://github.com/pytorch/pytorch/blob/b564eaf7a803ae7019ca0523d4ba8c060c30b0cb/torch/utils/data/sampler.py#L107), and it seems RandomSampler is just using the length of the `data_source` argument and the samples has nothing to do with `data_source`, can anyone help me understand what is the meaning of the argument `data_source`?

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/d/1/d192729b84c48bea72cbe0692ec340eb60f50cb0.png)

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [February 2, 2020, 3:36am UTC](https://discuss.pytorch.org/t/how-to-use-randomsampler/68376/2 "2020-02-02T03:36:37Z")

</div>

Judging from the source code of `RandomSampler`, `data_source` is only there to provide the number of elements ☹. If u need to sample from some specific indexes, I guess writing your own Sampler would work, it’s not very hard.
