randn!
randn!([rng], A::Array{Float64,N})
Fill the array A
with normally-distributed (mean 0, standard deviation 1) random numbers. Also see the rand function.
Examples
julia> rng = Random.MersenneTwister(1234); # Optional: specify the random number generator
julia> arr = zeros(3, 2);
julia> randn!(rng, arr)
3×2 Array{Float64,2}:
0.867347 0.972284
-0.901042 -0.937167
-0.494419 -0.767108
This example generates normally distributed random numbers using the randn!
function and fills the array arr
with the generated numbers.
julia> arr = [1.0, 2.0, 3.0, 4.0];
julia> randn!(arr)
4-element Array{Float64,1}:
0.3718436994071754
-1.6695509607077415
-0.07698343753913995
0.20169833389049528
In this example, the randn!
function fills the given array arr
with normally distributed random numbers.
Common mistakes:
julia> arr = [1, 2, 3];
julia> randn!(arr)
ERROR: MethodError: no method matching randn!(::Array{Int64,1})
The randn!
function generates normally distributed random numbers, which are of type Float64
. In this example, the array arr
contains integers, and therefore, the function call results in a MethodError
. Make sure the array type is compatible with the expected type for randn!
.
See Also
bitrand, MersenneTwister, rand, randcycle, randexp, randexp!, randjump, randn, randn!, RandomDevice, randperm, randsubseq, randsubseq!, shuffle, srand,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.