Computer Science
Write a function that takes a number n and then returns a randomly generated number having exactly n digits (not starting with zero) e.g., if n is 2 then function can randomly return a number 10-99 but 07, 02 etc. are not valid two digit numbers.
Python
Python Functions
9 Likes
Answer
import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return random.randint(lower_bound, upper_bound)
n = int(input("Enter the value of n:"))
random_number = generate_number(n)
print("Random number:", random_number)
Output
Enter the value of n:2
Random number: 10
Enter the value of n:2
Random number: 50
Enter the value of n:3
Random number: 899
Enter the value of n:4
Random number: 1204
Answered By
1 Like
Related Questions
Write a function that receives two string arguments and checks whether they are same-length strings (returns True in this case otherwise False).
Write a function namely nthRoot( ) that receives two parameters x and n and returns nth root of x i.e., x^(1/n).
The default value of n is 2.Write a function that takes two numbers and returns the number that has minimum one's digit.
[For example, if numbers passed are 491 and 278, then the function will return 491 because it has got minimum one's digit out of two given numbers (491's 1 is < 278's 8)].
Write a program that generates a series using a function which takes first and last values of the series and then generates four terms that are equidistant e.g., if two numbers passed are 1 and 7 then function returns 1 3 5 7.