# Random sampling of sorted numbers within range

**URL:** <https://discourse.pymc.io/t/random-sampling-of-sorted-numbers-within-range/5856>\
**Category:** Questions\
**Created:** [September 26, 2020, 7:41am UTC](https://discourse.pymc.io/t/random-sampling-of-sorted-numbers-within-range/5856 "2020-09-26T07:41:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vian](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vian/32/4300_2.png) [@vian](https://discourse.pymc.io/u/vian)\
**Post date:** [September 26, 2020, 7:41am UTC](https://discourse.pymc.io/t/random-sampling-of-sorted-numbers-within-range/5856/1 "2020-09-26T07:41:36Z")

</div>

Hi there,

I need your brains again for what I thought was a simple problem but turns out to be more complicated than I thought…

What I need is to be able to draw N numbers between 0 and 1, and they need to be sorted (any way). The following apparently works:

wtot = 1  
b = pm.Dirichlet(‘b’, a=np.ones(n\_sectors))  
w = [pm.Deterministic(‘w\_s{}’.format(i), wtot\*b[i]) for i in range(n\_sectors)]  
for s in np.arange(1, n\_sectors):  
pm.Potential(‘prior\_w\_s{}’.format(s), pm.Uniform.dist(lower=0).logp(w[s-1]-w[s]))

(wtot is in case we wanna draw up to any number below 1, and it can be a random variable too, here I just set it to 1)

But I have 2 questions:

- I would assume the use of potential slows down how fast draws are accepted/rejected, is that right? In which case:
- Is there a better solution?

Thanks for your help!  
Cheers,  
MV

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [October 3, 2020, 7:48am UTC](https://discourse.pymc.io/t/random-sampling-of-sorted-numbers-within-range/5856/2 "2020-10-03T07:48:52Z")

</div>

You might find some previous discussion helpful here:

> [@Sampling uniformly in a triangular support](https://discourse.pymc.io/t/sampling-uniformly-in-a-triangular-support/765/2):
>
> There are a couple of ways to do it: You can use the ordered transformation [here](https://discourse.pymc.io/t/mixture-models-and-breaking-class-symmetry/208/5). You can add jacobian correction for the volume change in your case above as well to make it proper. You can jointly sample from a bivariate standard Gaussian then rotating them by 45 degrees. The rotated sample is transformed into rates that lie in the unit square. See [here](http://nbviewer.jupyter.org/github/pymc-devs/resources/blob/master/BCM/ModelSelection/ComparingBinomialRates.ipynb#9.2-Order-restricted-equality-of-proportions). You can add a potential to restrict the order, see [here cell [4]](http://docs.pymc.io/notebooks/gaussian_mixture_model.html?highlight=potential) for an example

TL;dr:

```python
tr = pm.transforms
Order = tr.Ordered()
Logodd = tr.LogOdds()
chain_tran = tr.Chain([Logodd, Order])

n = 5
with pm.Model() as m0:
    x = pm.Uniform("x", 0.0, 1.0, shape=n, transform=chain_tran,
                   testval=np.linspace(.1, .9, n)) # test value need to be ordered
    trace = pm.sample()

```

---

<div class="post-metadata">

**Author:** ![vian](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vian/32/4300_2.png) [@vian](https://discourse.pymc.io/u/vian)\
**Post date:** [October 6, 2020, 10:54am UTC](https://discourse.pymc.io/t/random-sampling-of-sorted-numbers-within-range/5856/3 "2020-10-06T10:54:18Z")

</div>

Thanks! Right now I can’t make the Ordered transform work with Dirichlet (sum!=1) but that’s a really useful example for Uniform.

Cheers,  
MV
