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 a variety of reasons, such as creating unique identifiers, tracking dates, or displaying the date in a specific format. By using some simple programming techniques, you can quickly create this effect with minimal coding knowledge. Let's explore how to do it in JavaScript.

Let’s dive into the article to get a better understanding of displaying today's year and a random 4-digit number at the same time.

Using JavaScript random() method

The Math.random() static method provides a floating-point, pseudo-random number with a roughly uniform distribution over that range, larger than or equal to 0 and less than 1, which you can scale to your desired range. The random number generation algorithm's initial seed is chosen by the implementation; the user cannot change or reset it.

Syntax

Following is the syntax for random() -

Math.random()

Example

In the following example, we are running the script using the random() method for generating random numbers and the getFullYear() method to return the year in 4 digits.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         const x = new Date();
         let year =x.getFullYear();
         document.getElementById("tutorial").innerHTML ='Todays year:'+ year;
         var val = Math.floor(1000 + Math.random() * 9000);
         document.write('Random 4 Digit number: '+val);
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of today's year along with a 4-digit random number displayed on the webpage. The random number will keep changing as long as the user executes the script.

Using JavaScript slice() method

The slice() method returns a shallow copy of a subset of an array, where start and end are the indexes of the array's items, and the subset is chosen from start to end (end not included). The initial array won't be changed.

Syntax

Following is the syntax for slice()

array.slice(start, end)

Example

Consider the following example, where we are using the slice() method along with the random() method to get today's year along with a 4-digit random number.

<!DOCTYPE html>
<html>
   <body>
      <input type="text" id="tutorial" value="" disabled />
      <script>
         const now = new Date();
         let randomNum = '';
         randomNum += Math.round(Math.random() * 9);
         randomNum += Math.round(Math.random() * 9);
         randomNum += now.getTime().toString().slice(-2);
         document.getElementById("tutorial").value = `${new Date().getFullYear()}${randomNum}`;
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the disabled input field consisting of today’s year in four digits along with a four-digit random number displayed on the webpage. The random number will keep changing as soon as the user executes the script.

Updated on: 20-Apr-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements