To get a sample of 5 elements from an array with replacement in Python, you can use the random.choices()
function from the random
module.
Here's an example code block that randomly selects 5 elements from an array with replacement:
main.py112 chars7 lines
In this code block, we first import the random
module.
We then create an array my_array
with 10 elements.
Next, we use the random.choices()
function with 2 arguments: the array from which to sample (my_array
) and the number of elements to select (k=5
). This will return a list of 5 randomly selected elements from the array, with replacement.
Finally, we print the sample
list to the console.
gistlibby LogSnag