Importing randon error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Importing randon error (/thread-18848.html) |
Importing randon error - bharat_s579 - Jun-03-2019 Am trying to generate 2 random lists using below code import random a = random.randint(range(1,10)) b = random.randint(range(1,15)) c = [i for i in b if i in a] print(c)And while running giving below error. What i did wrong ?
RE: Importing randon error - michalmonday - Jun-03-2019 random.randint requires 2 input arguments so that's what error indicates. Input arguments supposed to be 2 integers. But there's another issue, it returns single value. You could either use list comprehension to produce a list or use the numpy.random.randint instead which returns the whole array.# settings low_border = 1 # inclusive high_border = 100 # inclusive list_size = 30 # 1st way import random a = [random.randint(low_border, high_border) for _ in range(list_size)] b = [random.randint(low_border, high_border) for _ in range(list_size)] c = [i for i in b if i in a] print('1ST WAY:', a, b, c, sep='\n\n') print('\n\n\n') # 2nd way import numpy as np np_a = np.random.randint(low_border, high_border + 1, list_size) np_b = np.random.randint(low_border, high_border + 1, list_size) np_c = np.intersect1d(np_a, np_b) # it sorts the values (idk if that's desired) print('2ND WAY:', np_a, np_b, np_c, sep = '\n\n')
|