Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing randon error
#1
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 ?

Output:
C:\Users\ASUS\PycharmProjects\Pratice\venv\Scripts\python.exe C:/Users/ASUS/.PyCharmCE2019.1/config/scratches/Pratice.py Traceback (most recent call last): File "C:/Users/ASUS/.PyCharmCE2019.1/config/scratches/Pratice.py", line 2, in <module> a = random.randint(range(1,10)) TypeError: randint() missing 1 required positional argument: 'b' Process finished with exit code 1
Reply
#2
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')
Output:
1ST WAY: [75, 53, 99, 84, 40, 76, 79, 64, 94, 95, 56, 73, 20, 94, 24, 56, 100, 66, 2, 92, 71, 84, 44, 58, 8, 12, 7, 45, 49, 62] [64, 80, 91, 69, 16, 73, 4, 26, 46, 6, 64, 24, 61, 38, 39, 85, 94, 56, 38, 50, 44, 96, 100, 16, 52, 30, 47, 3, 15, 87] [64, 73, 64, 24, 94, 56, 44, 100] 2ND WAY: [80 73 45 52 46 65 74 72 28 74 54 80 92 23 27 60 66 51 73 42 47 15 71 68 91 4 25 36 44 44] [ 91 67 74 99 58 86 89 49 45 92 78 6 22 26 51 90 88 31 9 75 75 73 42 15 100 86 41 86 29 66] [15 42 45 51 66 73 74 91 92]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyarrow error when importing pandas sravva 1 884 Jun-06-2023, 05:09 PM
Last Post: snippsat
  ERROR: importing desired module mbgamer28 0 1,648 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
Bug Error while importing numpy Erfan 3 3,219 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  Importing module from a package results in import error goghvv 2 2,340 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Error importing package julio2000 3 3,709 Jan-26-2020, 06:15 PM
Last Post: buran
  Error in importing numpy fullstop 1 1,887 Dec-18-2019, 12:07 PM
Last Post: jefsummers
  Error in importing package in pycharm fullstop 0 2,314 Dec-12-2019, 04:03 PM
Last Post: fullstop
  Importing module error walernor 5 3,295 Feb-02-2019, 04:14 PM
Last Post: ichabod801
  Error while importing my own package giu88 4 2,911 Sep-19-2018, 12:18 PM
Last Post: giu88
  Error message when importing jsonlines library vakil67 5 7,265 May-16-2018, 08:51 PM
Last Post: vakil67

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020