rand

rand([rng], [S], [dims...])

Pick a random element or array of random elements from the set of values specified by S; S can be

  • an indexable collection (for example 1:n or ['x','y','z']), or
  • a type: the set of values to pick from is then equivalent to typemin(S):typemax(S) for integers (this is not applicable to BigInt), and to $[0, 1)$ for floating point numbers;

S defaults to Float64.

Examples

julia> rand()
0.41545291085770697
julia> rand(Uint32)
0x96d887ee
julia> rand(3)
3-element Array{Float64,1}:
 0.806615
 0.974651
 0.380263
julia> rand(Int8,2,2)
2x2 Array{Int8,2}:
 -54    107
 -1     -100
 julia> rand(0:10)
 5
 julia> rand(0:2:20, 4)
 4-element Array{Int64,1}:
  2
  10
  20
  6
julia> A = rand(10,5)
10x5 Array{Float64,2}:
 0.694826  0.953467   0.813771   0.852055  0.261838
 0.825119  0.0781648  0.163994   0.890056  0.968585
 0.597318  0.614048   0.953451   0.828575  0.714666
 0.782182  0.158034   0.0773286  0.631305  0.699102
 0.30093   0.701959   0.57233    0.283952  0.43863 
 0.804297  0.580577   0.731646   0.639573  0.243464
 0.564545  0.596057   0.939858   0.133461  0.583886
 0.168195  0.839508   0.128291   0.851288  0.796833
 0.253853  0.95848    0.950334   0.199548  0.163088
 0.722749  0.913511   0.459316   0.108941  0.102196
  1. Generate a single random number:

    julia> rand()
    0.8975592506457546

    This example generates a single random number between 0 and 1.

  2. Generate a random integer within a specific range:

    julia> rand(1:100)
    42

    It generates a random integer between 1 and 100 (inclusive).

  3. Generate an array of random numbers with specific dimensions:

    julia> rand(3, 2)
    3×2 Matrix{Float64}:
    0.450378  0.438895
    0.965156  0.330876
    0.513338  0.114617

    This example generates a 3x2 matrix filled with random numbers.

  4. Generate a random number using a specific random number generator:

    julia> rng = MersenneTwister(1234);
    julia> rand(rng)
    0.5908446386657106

    It uses a specific random number generator (MersenneTwister in this case) to generate a random number.

  5. Generate an array of random numbers using a specific random number generator and dimensions:

    julia> rng = MersenneTwister(1234);
    julia> rand(rng, 2, 2)
    2×2 Matrix{Float64}:
    0.590844  0.766797
    0.880904  0.479388

    This example uses the MersenneTwister random number generator to generate a 2x2 matrix of random numbers.

  6. Generate random numbers within a specific range:
    julia> rand(2:5, 3)
    3-element Array{Int64,1}:
    4
    4
    3

    It generates an array of three random integers between 2 and 5 (inclusive).

Note: rand() without any arguments generates random numbers of type Float64 between 0 and 1.

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.

*Required Field
Details

Checking you are not a robot: