# How can i create a random signal within a fixed range ±1e-6?

**URL:** <https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474>\
**Category:** New to Julia\
**Created:** [January 6, 2025, 5:46pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474 "2025-01-06T17:46:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefaniecg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefaniecg/32/204815_2.png) [@stefaniecg](https://discourse.julialang.org/u/stefaniecg)\
**Post date:** [January 6, 2025, 5:46pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/1 "2025-01-06T17:46:36Z")

</div>

This one is close, but it goes above the ±1e-6.

```julia
signal = randn(Int(1e6)) * 1e-6
plot(signal)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 6, 2025, 5:48pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/2 "2025-01-06T17:48:46Z")

</div>

White noise can never have a fixed amplitude. It can have a more-or-less fixed standard deviation, though.

---

<div class="post-metadata">

**Author:** ![stefaniecg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefaniecg/32/204815_2.png) [@stefaniecg](https://discourse.julialang.org/u/stefaniecg)\
**Post date:** [January 6, 2025, 5:49pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/3 "2025-01-06T17:49:25Z")

</div>

i need such a signal, could you help me create it ?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 6, 2025, 5:49pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/4 "2025-01-06T17:49:41Z")

</div>

It is mathematically impossible.

Why do you need such a signal? I think you do not understand the concept of noise.

## Amplitude Distribution

The amplitude of white noise at any given moment is random, but follows certain statistical properties:

1. Zero mean: The average of a white noise signal over time is zero[  
 ](https://en.wikipedia.org/wiki/White_noise_(slang))
2. Finite variance: The amplitude variations have a limited range[  
 ](https://en.wikipedia.org/wiki/White_noise_(slang))

It’s important to note that while the instantaneous amplitude of white noise fluctuates randomly, its overall statistical properties remain constant.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 6, 2025, 5:57pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/5 "2025-01-06T17:57:53Z")

</div>

This depends on what you mean by ‘random’. If you require normally distributed noise (which is what `randn` returns), then you _cannot_ guarantee a particular upper limit. There are other distributions, like the uniform distribution whis _is_ limited.

You _could_ use `randn` and then clip the data at you desired limit, but it would no longer be normal.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 6, 2025, 6:06pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/6 "2025-01-06T18:06:02Z")

</div>

So perhaps you want:

```julia
signal = (rand(10^6) .- 0.5) * 2e-6
plot(signal)

```

A uniform distribution with the amplitude of 1.0.

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [January 6, 2025, 6:12pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/7 "2025-01-06T18:12:04Z")

</div>

Why the Int?

```julia
julia> extrema(2e-6*(rand(10^7) .- 0.5))
(-9.999999081819787e-7, 9.999999401906048e-7)

julia> extrema(2e-6*(randn(10^7) .- 0.5))
(-1.1699182863351871e-5, 9.982354577899941e-6)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 6, 2025, 6:24pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/8 "2025-01-06T18:24:32Z")

</div>

Not working for me:

```julia
julia> signal = (rand(1e6) .- 0.5) * 2e-6
ERROR: MethodError: no method matching Random.Sampler(::Type{Random.TaskLocalRNG}, ::Random.SamplerTrivial{Float64, Float64}, ::Val{1})

Closest candidates are:
  Random.Sampler(::Type{<:Random.AbstractRNG}, ::Random.Sampler, ::Union{Val{1}, Val{Inf}})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:147
  Random.Sampler(::Type{<:Random.AbstractRNG}, ::Any, ::Union{Val{1}, Val{Inf}})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:183
  Random.Sampler(::Type{<:Random.AbstractRNG}, ::BitSet, ::Union{Val{1}, Val{Inf}})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/generation.jl:450
  ...

Stacktrace:
 [1] Random.Sampler(T::Type{Random.TaskLocalRNG}, sp::Random.SamplerTrivial{Float64, Float64}, r::Val{1})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:147
 [2] Random.Sampler(rng::Random.TaskLocalRNG, x::Random.SamplerTrivial{Float64, Float64}, r::Val{1})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:139
 [3] rand(rng::Random.TaskLocalRNG, X::Random.SamplerTrivial{Float64, Float64})
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:255
 [4] rand(rng::Random.TaskLocalRNG, X::Float64)
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:255
 [5] rand(X::Float64)
   @ Random ~/.julia/juliaup/julia-1.10.7+0.x64.linux.gnu/share/julia/stdlib/v1.10/Random/src/Random.jl:260
 [6] top-level scope
   @ REPL[1]:1

```

OK, I see, `1e10` is different from `10^10`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 6, 2025, 7:32pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/9 "2025-01-06T19:32:14Z")

</div>

> [@ufechner7](#):
>
> White noise can never have a fixed amplitude. It can have a more-or-less fixed standard deviation, though.

You probably mean “Gaussian noise”, as opposed to “white noise” meaning “has a flat spectrum”. White noise does not have to be Gaussian.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 6, 2025, 7:34pm UTC](https://discourse.julialang.org/t/how-can-i-create-a-random-signal-within-a-fixed-range-1e-6/124474/10 "2025-01-06T19:34:43Z")

</div>

> [@DNF](#):
>
> You _could_ use `randn` and then clip the data at you desired limit, but it would no longer be normal.

You can also use Distributions.jl here — it’s always nice to use something established as opposed to hand-rolling it yourself, especially when it comes to random distributions.

In the language of a distribution, clipping is a `censored` distribution:

```julia-repl
julia> using Distributions, Plots

julia> histogram(rand(censored(Normal(0, 1e-6/3), -1e-6, 1e-6), 10^6))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/4/543db9d2c88ee2d0751c1187d6122535c098fd16.png)

Or, you could truncate the distribution, effectively resampling those points from outside the range you wanted:

```julia-repl
julia> histogram(rand(truncated(Normal(0, 1e-6/3), -1e-6, 1e-6), 10^6))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/2/a2e9968f284c42fb31af852abecc29e0228ca9e7.png)

Or you could look through Distribution.jl’s docs and find any other shape that fits your fancy.
