0

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

Priya BB
  • 3
  • 5

1 Answers1

0

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)
mdf
  • 133
  • 8
  • it gives just one row in output randomly every time I want the row same as in this array A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and also the rows which have smaller in some will be omitted like 1+2+3 = 6 it will be omitted 3 new rows will be generated randomly from remaining two rows of array A – Priya BB Dec 16 '21 at 13:12
  • Updated answer. – mdf Dec 16 '21 at 15:31