# What is the difference between rand and sample?

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 23, 2020, 9:53pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297 "2020-12-23T21:53:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 23, 2020, 9:53pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/1 "2020-12-23T21:53:00Z")

</div>

What is the difference between `rand(array, n)` and `sample(array, n)`?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 23, 2020, 10:48pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/2 "2020-12-23T22:48:51Z")

</div>

`sample` is from where?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 23, 2020, 10:52pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/3 "2020-12-23T22:52:06Z")

</div>

> `sample` is from where?

`StatsBase.sample(array, n)` and `StatsBase.rand(array, n)`

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [December 23, 2020, 11:01pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/4 "2020-12-23T23:01:29Z")

</div>

`rand` is from `Base`.

I don’t know how consistent people are about this, but as I think of it, `rand` is usually lower-level, while `sample` is often built in terms of (potentially many) calls to `rand`. `sample` is also often used for MCMC.

@Tamas_Papp I’ve heard you make similar arguments, anything to add?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 23, 2020, 11:18pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/5 "2020-12-23T23:18:31Z")

</div>

If you use `@edit` to inspect the code of calls to both functions you will probably reach the same understanding given by @cscherrer. It seems like `sample` calls a `sample!` that may call many different options, ranging from `direct_sample` (which then calls `rand`) or things like: `samplepair`, `fisher_yates_sample!`, `self_avoid_sample!`, and `seqsample_c!` (more complex ways of sampling, all probably built over `rand`).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 24, 2020, 7:12am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/6 "2020-12-24T07:12:09Z")

</div>

`StatsBase.sample` is for picking random items from a collection, with potentially weights and with/without replacement. I don’t think it was intended to be a generic function, it is a utility for a special case.

`Random.rand` is a generic IID sampler.

@jzr, all of this is documented, which part of the documentation did you find unclear?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 24, 2020, 8:10am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/7 "2020-12-24T08:10:07Z")

</div>

> I don’t think it was intended to be a generic function, it is a utility for a special case.

My confusion arose because `sample` is indeed used as a generic function by Turing.jl and other packages. If I understand correctly, there are multiple interpretations of the distinction between these functions. (Namely, “rand is generic iid; sample isn’t generic”, and “rand is simple low-level drawing; sample is higher-level drawing that uses rand”.)

Would it have been appropriate usage for the MCMC packages to use `rand` for their user-facing sampling interface (instead of `sample`)?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 24, 2020, 9:15am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/8 "2020-12-24T09:15:48Z")

</div>

Personally I think that using `StatsBase.sample` for MCMC is bad style (“punning”).

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 24, 2020, 12:53pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/9 "2020-12-24T12:53:18Z")

</div>

I fully agree with this view. The function `sample` refers to an operation performed on a dataset (or population) and can take weights and replacement options. The function `rand` is for random variables with a given distribution (e.g. `Distributions.jl`).

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [December 24, 2020, 3:05pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/10 "2020-12-24T15:05:37Z")

</div>

@Tamas_Papp @juliohm what name would you use for a function that samples from a posterior distribution, given a model, observed data, and sampling algorithm?

Soss currently uses a different function name for each algorithm, but I like the idea of having the abstracted away a bit, so making the algorithm an argument is appealing to me.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 24, 2020, 3:32pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/11 "2020-12-24T15:32:52Z")

</div>

I think that `Turing.sample` is OK. It just should not coincide with `StatsBase.sample`, which is a rather pointless pun.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 24, 2020, 3:45pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/12 "2020-12-24T15:45:24Z")

</div>

@cscherrer I think I would end up choosing `rand` for this posterior distribution so that it is clear that it is not a sample from a population a la `StatsBase.sample`. I am trying to follow this pattern in my packages whenever I can so that users know when they can pass weights for example.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 24, 2020, 3:51pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/13 "2020-12-24T15:51:20Z")

</div>

> [@cscherrer](#):
>
> what name would you use for a function that samples from a posterior distribution, given a model, observed data, and sampling algorithm?

And I would maybe describe this functionality as:

> A function that draws from a posterior distribution given a model, observed data and algorithm…

But that is all a matter of taste I guess as we all understand the meaning of these concepts. It would be nice to have a consistent terminology across multiple ecosystems though. 🙂

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [December 24, 2020, 6:06pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/14 "2020-12-24T18:06:05Z")

</div>

> [@Tamas\_Papp](#):
>
> I think that `Turing.sample` is OK. It just should not coincide with `StatsBase.sample`

I’m pretty sure Turing overrides and re-exports `StatsBase.sample`.

> [@juliohm](#):
>
> @cscherrer I think I would end up choosing `rand` for this posterior distribution so that it is clear that it is not a sample from a population a la `StatsBase.sample` .

But these samples are not usually iid, which breaks a common assumption of `rand`. To me this is a more critical distinction than requiring that `sample` is only for drawing a sample from a population, which isn’t at all clear to me. Are you requiring here that a “population” be finite, and unweighted?

If someone uses `rand` thinking the results will be iid, violating that could cause big problems. As for `sample`, I really don’t see such a risk in extending it.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 24, 2020, 6:24pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/15 "2020-12-24T18:24:59Z")

</div>

> [@cscherrer](#):
>
> But these samples are not usually iid, which breaks a common assumption of `rand` .

Oh I had IID samples in mind when you wrote:

> [@cscherrer](#):
>
> a function that samples from a posterior distribution

I am assuming they are not IID because of an algorithmic (MCMC) detail?

> [@cscherrer](#):
>
> Are you requiring here that a “population” be finite, and unweighted?

Yes, and I think that is what most people have in mind when they hear the term population in statistics, but I may be wrong.

I think the IID property is relevant, but isn’t the critical aspect to differentiate between `rand` and `sample` semantics.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [December 24, 2020, 6:29pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/16 "2020-12-24T18:29:34Z")

</div>

> [@juliohm](#):
>
> I am assuming they are not IID because of an algorithmic (MCMC) detail?

Yes, that’s right. Sometimes we can get IID samples, but that’s not usually the case.

> [@juliohm](#):
>
> Yes, and I think that is what most people have in mind when they hear the term population in statistics, but I may be wrong.

Maybe it depends on which branch of statistics. The expression “sample from the posterior” comes up _a lot_ in Bayesian stats, and we usually don’t assume the result will be IID.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 26, 2020, 8:26am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/17 "2020-12-26T08:26:18Z")

</div>

> [@cscherrer](#):
>
> Maybe it depends on which branch of statistics. The expression “sample from the posterior” comes up _a lot_ in Bayesian stats, and we usually don’t assume the result will be IID.

That said, `StatsBase.sample` is a utility function for a very specific stats/probability exercise, ie pulling objects from a bag/urn with/without replacement. This meaning predates the Bayesian/MCMC usage by centuries, and is really not the same thing.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 29, 2025, 1:10pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/18 "2025-01-29T13:10:07Z")

</div>

I would like to revive this topic for two reasons:

1. We should try to be consistent terminology-wise and I agree with @Tamas_Papp that the term `sample` was introduced way before McMC, Turing, etc. If Bayesian packages need a name that is not `Base.rand`, they could perhaps adopt `draw(posterior)` and own the name `draw` as part of their API. It would be better than `sample` at least.

2. Is there a lightweight, actively-maintained alternative to StatsBase.jl nowadays when it comes to sampling with weights and w/o replacement?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 29, 2025, 6:14pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-rand-and-sample/52297/19 "2025-01-29T18:14:17Z")

</div>

> [@juliohm](#):
>
> If Bayesian packages need a name that is not `Base.rand`, they could perhaps adopt `draw(posterior)` and own the name `draw` as part of their API.

Given that all the APIs are different (at this point), I am not sure that a common verb makes sense.

In any case, DynamicHMC.jl does not use `sample` as part of its API, I have chosen more expressive names (I hope) like `mcmc_with_warmup`.
