code to initialize a sequence

joseph pareti joepareti54 at gmail.com
Sun Aug 29 14:44:53 EDT 2021


In the code attached below, the A-variant is from somebody else who knows
Python better than I. But I do not like to just use any code without having
a grasp, specifically the line in* bold*, so I wrote the B-variant which
gives the same results. The C-variant is identical to A and is there for
verification: after resetting the seed I expect the same sequence. The
D-variant is closer to the way I code, and it does not work.


import random
from random import randint, seed

def generate_sequence(length, n_unique):
*return [randint(0, n_unique-1) for k in range(length)]*

def generate_sequence_JP(length, n_unique):
   LI = []
   for k in range(length):
     LI.append(randint(0, n_unique-1))
   return(LI)
def generate_sequence_EXPLICIT(length, n_unique):
   X =[None] * length
      for i in range(length):
        X[i] = [randint(0, n_unique-1)]
   return X
#
# MAIN PROGRAM
#
random.seed(2)
A = generate_sequence(4, 10 )
random.seed(2)
B = generate_sequence_JP(4, 10)
random.seed(2)
C = generate_sequence(4, 10 )
random.seed(2)
D = generate_sequence_EXPLICIT(4, 10 )
print(A)
print(type(A))
print('-----------------------------')
print(B)
print(type(B))
print('-----------------------------')
print(C)
print(type(C))
print('-----------------------------')
print(D)
print(type(D))


Regards,
Joseph Pareti - Artificial Intelligence consultant
Joseph Pareti's AI Consulting Services
https://www.joepareti54-ai.com/
cell +49 1520 1600 209
cell +39 339 797 0644


More information about the Python-list mailing list