RandomDevice
RandomDevice()
Create a RandomDevice
RNG object. Two such objects will always generate different streams of random numbers.
Examples
-
Create a RandomDevice object:
julia> rng = RandomDevice() RandomDevice() julia> rand(rng, 3) 3-element Array{Float64,1}: 0.8001107062114954 0.4821127743457391 0.6071435902237073
This example creates a
RandomDevice
object namedrng
. You can then use therng
object to generate random numbers using therand
function. -
Generate random integers using RandomDevice:
julia> rng = RandomDevice() RandomDevice() julia> rand(rng, 1:10, 5) 5-element Array{Int64,1}: 5 8 3 3 2
Here, the
rand
function is used with theRandomDevice
objectrng
to generate an array of 5 random integers between 1 and 10 (inclusive). -
Generate a random permutation using RandomDevice:
julia> rng = RandomDevice() RandomDevice() julia> randperm(rng, 5) 5-element Array{Int64,1}: 2 5 3 1 4
In this example, the
randperm
function is used with theRandomDevice
objectrng
to generate a random permutation of the numbers from 1 to 5.
Common mistake example:
julia> rng = RandomDevice
ERROR: MethodError: no method matching RandomDevice()
In this example, the mistake made is not calling the RandomDevice
constructor with parentheses. It is important to use RandomDevice()
to create a RandomDevice
object, not just RandomDevice
.
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.