# How to generate normalized random floats in range (0;1)

**URL:** https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852
**Category:** General Usage
**Tags:** question, random
**Created:** [July 6, 2022, 7:50pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852 "2022-07-06T19:50:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)
#### Post date: [July 6, 2022, 7:50pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852/1 "2022-07-06T19:50:34Z")

</div>

I need to generate normal random floats in range [0;1], but randn(Float64) uses a different range.

How to fix it?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [July 6, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852/2 "2022-07-06T19:56:58Z")

</div>

The normal distribution has infinite support (in theory). `randn` samples a normal distribution with variance `1` and mean `0` (so most values are in `[-1, 1]` and very few values are outside `[-5, 5]`). If you want to reshape this, you can always add and multiply: almost all outputs of `randn()/5 + 0.5` will be in your suggested range.

---

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [July 6, 2022, 8:29pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852/3 "2022-07-06T20:29:52Z")

</div>

In the normal distribution, most values are within [-0.6744897501960818, 0.6744897501960818]… 😁

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 6, 2022, 10:12pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852/4 "2022-07-06T22:12:46Z")

</div>

If you sample from a distribution but throw away any values outside a range, you effectively sample a _truncated distribution_. I must emphasize that a truncated distribution is not the same as the original distribution, but it’s what you use if you really need a strict range. Distributions.jl [supports truncation](https://juliastats.org/Distributions.jl/stable/truncate/) of univariate distributions, and you use `rand` to sample. As @gustaphe said, you could truncate to [-a, a] and adjust the values to the range [0, 1], or you could also adjust the mean and variance of the `Normal` distribution before you truncate it to [0, 1].

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [July 7, 2022, 5:22pm UTC](https://discourse.julialang.org/t/how-to-generate-normalized-random-floats-in-range-0-1/83852/5 "2022-07-07T17:22:38Z")

</div>

Both statements are true 😃.

I normally go by the 68-95-99.7 rule, but I never remember the specific numbers so I call it “most”, “almost all”, “pretty much all”.
