How to select random rows from a matrix in python scratch without any numpy library

268 Views Asked by At

Hi i am trying to inplememt Genetic algorithm rollete wheel selection mechanism. I have done fitness and also the corresponding fitness probability. I need a code to randomly select rows from initial generated matrix.but only using scratch python bc i am not using any libary such as numpy or jupyter

Same like this one but without numpy

Select two random rows from numpy array

Thank you

1

There are 1 best solutions below

2
mdf On

Use random.sample():

import random  # for random.sample

# This is an example matrix
A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]


num_random_rows_desired = 2
a = random.sample(A, num_random_rows_desired)
print(a)