VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-generate-a-random-two-digit-number/

⇱ Program to generate a random two-digit number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to generate a random two-digit number

Last Updated : 18 Jan, 2024

Write a program to generate a random two-digit number.

Example 1: 73
Example 2: 26

Approach: To solve the problem, follow the below idea:

We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 to 89, so in order to have the remainder as a two-digit number, we can further add 10 to make the range 10 to 99.

Step-by-step algorithm:

  • Seed the random number generator to ensure different random numbers on each run.
  • Generate a random number within the range of two-digit numbers (10 to 99).

Below is the implementation of the algorithm:


Output
Generated Random Number: 47

Time Complexity: O(1), it takes constant time to generate a random number.
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: