How do you generate a 4 digit random number in Python?
This will include any four digit from 0000, not just 1000. It can also be made a string rather than a value.
Answer 51251e0348181daeeb001886
Answer 51253334f08b0079cf002382
0 votes
import random rand = random.choice(‘0123456789’) # Chooses a random element rand1 = random.choice(‘0123456789’) rand2 = random.choice(‘0123456789’) rand3 = random.choice(‘0123456789’) print rand + rand1 + rand2 + rand3
This will give you a fairly random value and just add an if else statement if you dont want 1000, if the result is 1000, just make it go in the loop once more.
PS: i have just started out, give i will shorten the code as i learn more, right now it takes 5 steps, i think there is a way to do it in 2.
1 comments
You should add all the rand variables to one variable and print that for use in further code.
Popular free courses
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.4 Lessons
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.11 Lessons
- Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
- Beginner Friendly.6 Lessons