Profile image of kdanilczuk
Submitted by kdanilczuk
over 13 years

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

0 votes

Permalink

Challenge accepted :) Will post code when I’v figured it out ;)

Profile image of vpeluso93
Submitted by vpeluso93
over 13 years

Answer 51253334f08b0079cf002382

0 votes

Permalink

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.

Profile image of ravij12
Submitted by ravij12
over 13 years

1 comments

Profile image of rubySurfer69935
Submitted by rubySurfer69935
over 13 years

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
Explore full catalog