8000 Use random.choice instead of random.randint · hpiquant/python-scripts@5b74528 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5b74528

Browse files
authored
Use random.choice instead of random.randint
random.choice is better for selecting random elements from a list than using random.randint to generate random indices to select
1 parent 8
8000
eb0735 commit 5b74528

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

scripts/13_random_name_generator.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from random import randint
1+
from random import choice
22

33

44
def random_name_generator(first, second, x):
@@ -10,13 +10,8 @@ def random_name_generator(first, second, x):
1010
- number of random names
1111
"""
1212
names = []
13-
for i in range(0, int(x)):
14-
random_first = randint(0, len(first)-1)
15-
random_last = randint(0, len(second)-1)
16-
names.append("{0} {1}".format(
17-
first[random_first],
18-
second[random_last])
19-
)
13+
for i in range(x):
14+
names.append("{0} {1}".format(choice(first), choice(second)))
2015
return set(names)
2116

2217

0 commit comments

Comments
 (0)
0