get a sample of 5 elements from an array with replacement in python

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.py
import random

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sample = random.choices(my_array, k=5)

print(sample)
112 chars
7 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