How to Show Today\'s Year And Random 4Digit Numbers at the Same Time?

Let's see "How to show the current year and a random 4-digit number at the same time." This can be useful for creating unique identifiers, tracking dates, or displaying dynamic content. By using JavaScript's built-in Date and Math objects, you can easily implement this functionality.

Let's explore different approaches to display today's year and a random 4-digit number simultaneously.

Using Math.random() Method

The Math.random() method returns a floating-point number between 0 (inclusive) and 1 (exclusive). We can scale this to generate 4-digit numbers.

Syntax

Math.random()

Example

In this example, we use Math.random() to generate a 4-digit number and getFullYear() to get the current year:

<!DOCTYPE html>
<html>
   <body>
      <p id="result"></p>
      <script>
         const currentDate = new Date();
         let year = currentDate.getFullYear();
         
         // Generate random 4-digit number (1000-9999)
         let randomNumber = Math.floor(1000 + Math.random() * 9000);
         
         document.getElementById("result").innerHTML = 
            `Today's year: ${year}<br>Random 4-digit number: ${randomNumber}`;
      </script>
   </body>
</html>
Today's year: 2024
Random 4-digit number: 7352

Using Combined Year and Random Number

This approach creates a single value combining the current year with a random 4-digit number:

<!DOCTYPE html>
<html>
   <body>
      <input type="text" id="combined" readonly style="width: 200px; padding: 5px;" />
      <button onclick="generateNew()">Generate New</button>
      <script>
         function generateCombined() {
            const year = new Date().getFullYear();
            const randomFourDigit = Math.floor(1000 + Math.random() * 9000);
            return `${year}-${randomFourDigit}`;
         }
         
         function generateNew() {
            document.getElementById("combined").value = generateCombined();
         }
         
         // Initialize on load
         generateNew();
      </script>
   </body>
</html>
2024-4817

Using Timestamp-Based Random Number

This method incorporates the current timestamp to ensure more unique random numbers:

<!DOCTYPE html>
<html>
   <body>
      <div id="display"></div>
      <script>
         const now = new Date();
         const year = now.getFullYear();
         
         // Use timestamp for more randomness
         const timestamp = now.getTime().toString();
         const lastFourDigits = timestamp.slice(-4);
         
         // Combine with additional random digits
         const randomPrefix = Math.floor(Math.random() * 100).toString().padStart(2, '0');
         const hybridRandom = randomPrefix + lastFourDigits.slice(-2);
         
         document.getElementById("display").innerHTML = 
            `<strong>Year:</strong> ${year}<br>` +
            `<strong>Random Number:</strong> ${hybridRandom}<br>` +
            `<strong>Combined:</strong> ${year}${hybridRandom}`;
      </script>
   </body>
</html>
Year: 2024
Random Number: 4267
Combined: 20244267

Comparison of Methods

Method Randomness Use Case
Math.random() High General purpose, simple IDs
Combined Format High User-friendly display
Timestamp-based Very High Unique identifiers, tracking

Conclusion

These methods provide flexible ways to display the current year with random 4-digit numbers. Use Math.random() for simplicity, or timestamp-based approaches for higher uniqueness in your applications.

Updated on: 2026-03-15T23:19:01+05:30

242 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements