Back

Implementing our algorithm in Python to generate data - Answers 

Here is the code for the program, which can be copied straight into Python:

import random # get the random number generator

myArray = [] # create an empty list
size = int(input('How many data items? >>> ')) # ask for the size required
for i in range(size): # for i= 0 to (size-1)
    myArray.append(random.randint(1,500)) # get a random number and append it

for i in range(size): # for i= 0 to (size-1)
    print(myArray[i],end=' ') # print what you find at that position

In these lines:

for i in range(size): # for i= 0 to (size-1)
      print(myArray[i],end=' ') # print what you find at that position 

i is used to pick out the data at each element in the line print(myArray[i],end=' ') # print what you find at that position

Back