randsubseq
randsubseq(A, p) -> Vector
Return a vector consisting of a random subsequence of the given array A
, where each element of A
is included (in order) with independent probability p
. (Complexity is linear in p*length(A)
, so this function is efficient even if p
is small and A
is large.) Technically, this process is known as "Bernoulli sampling" of A
.
Examples
-
Generate a random subsequence of an array:
julia> arr = [1, 2, 3, 4, 5]; julia> randsubseq(arr, 0.5) 2-element Array{Int64,1}: 3 4
This example generates a random subsequence of the array
arr
with each element being included with an independent probability of 0.5. -
Create a random subset of strings:
julia> words = ["apple", "banana", "orange", "grape"]; julia> randsubseq(words, 0.3) 1-element Array{String,1}: "orange"
It generates a random subsequence of the vector of strings
words
with each element being included with an independent probability of 0.3. - Generate a shuffled version of an array:
julia> numbers = [1, 2, 3, 4, 5]; julia> randsubseq(numbers, 1.0) 5-element Array{Int64,1}: 1 5 4 3 2
It generates a shuffled version of the array
numbers
where each element is included with a probability of 1.0.
Common mistake example:
julia> arr = [1, 2, 3, 4, 5];
julia> randsubseq(arr, 1.5)
ERROR: DomainError with 1.5:
Value must be in the range [0, 1]
In this example, an invalid probability value of 1.5 is provided. The probability p
should be in the range [0, 1]. Make sure to provide a valid probability value to avoid such errors.
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.