# Best way to generate random floats over a wide range of magnitudes

**URL:** https://discourse.julialang.org/t/best-way-to-generate-random-floats-over-a-wide-range-of-magnitudes/74129
**Category:** General Usage
**Tags:** rng
**Created:** [January 6, 2022, 11:08am UTC](https://discourse.julialang.org/t/best-way-to-generate-random-floats-over-a-wide-range-of-magnitudes/74129 "2022-01-06T11:08:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![swishmas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swishmas/32/8803_2.png) [@swishmas](https://discourse.julialang.org/u/swishmas)
#### Post date: [January 6, 2022, 11:08am UTC](https://discourse.julialang.org/t/best-way-to-generate-random-floats-over-a-wide-range-of-magnitudes/74129/1 "2022-01-06T11:08:50Z")

</div>

Simple question: `rand()` generally returns a random number rarely in the range (0,1E-1) and I am interested in a range of random numbers between (0,1E-2).

I was wondering if anyone could verify that a solution like

```julia
rand() * (10.)^(rand(-16:0,1)[1])

```

would do generate fair random numbers in this range. Or maybe there is a more elegant (or built-in) solution?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 6, 2022, 11:15am UTC](https://discourse.julialang.org/t/best-way-to-generate-random-floats-over-a-wide-range-of-magnitudes/74129/2 "2022-01-06T11:15:22Z")

</div>

If you want uniformly distributed numbers (as rand provides in the range 0 to 1) then you just scale:

```julia
rand() * 1e-2

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [January 6, 2022, 12:10pm UTC](https://discourse.julialang.org/t/best-way-to-generate-random-floats-over-a-wide-range-of-magnitudes/74129/3 "2022-01-06T12:10:15Z")

</div>

If you need numbers with their log10 uniformly distributed between -16 and 0, as I guessed from the line of code, they can be generated with `10 ^ (-16*rand())`.
