+ 1

1 line random number generator?

I'm a noob. Trying to write a program that will require lots of random things.. and trying to make it as short as possible... import random num = range(-1, 9) def randnum(): print(str(random.choice(num)) * 3) That gives me three times the same random number... 111, 333 etc.. def randnum(): num1 = str(random.choice(num) num2 = str(random.choice(num) num3 = str(random.choice(num) print(num1 + num2 + num3) That way works.. but it seems longer than it has to be.

1st Jun 2017, 7:38 AM
Spock
Spock - avatar
3 Réponses
0
Good grief that 512 character limit is a pain.. Which is why that post seems very underexplained lol. Im making a random password generator. Im writing multiple functions to generate each part of the password to varying strengths depending on user input. Im also trying to teach myself to get my code as compact and as efficient as possible.. I started with a random number generator function. But it seems like a long winded piece of code for such a simple task. Is there a way to write it in 1 lineor at least shorter than what I have?
1st Jun 2017, 7:43 AM
Spock
Spock - avatar
0
I have done something like that. If I remember correctly: ''.join(str(randint(-1,8)) for _ in range(3)) Checked, it works. You can modify it all you want (I used randint in accordance to your usage)
1st Jun 2017, 10:04 PM
Amaras A
Amaras A - avatar