How to Generate random data in JavaScript

How to Generate random String or data into Java Script. In this JavaScript exercise let’s create JS code to generate random data into bulk as per data type.

Problem statement

In this article, we will generate a random integer using the javascript function. For that, you have to create an HTML file with a JS function abc.html JS function that will return the random number and print the generated random number on the console browser.

Solution

  • Create HTML file as .html
  • Use math.random()
  • Run program Display results in the console

JS code to display random number using JS

<!DOCTYPE html>
<html>
<head>
 
  <title>Javascript random function</title>
</head>
<body>
 <script type="text/javascript">
  var x = Math.random();
  x = x * (10-1)+1;
 console.log(" Random number: "+ Math.floor(x));
 </script>
</body>
</html>

Output

 

In this way, we learned how to display random numbers using JS.

Js code to generate a random string

Problem statement

In this, you have to generate a random string using the javascript function. For that create an HTML file as xyz.html with JS functions that will return the random string in capital or small letter with x length and print the generated string output on the console.

Solution

  • Create HTML file
  • You have given a string of characters
  • Use function  function makeid(length)
  • Use for loop for ( var i = 0; i < length; i++ )
  • Run HTML file

Js code to generate a random string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Generate random string </title>
</head>
<body>
<script>
    function makeid(length) {
    var result           = '';
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * 
 charactersLength));
   }
   return result;
}

console.log(makeid(5));
</script>
</body>
</html>

Output

 

Js code to generate random dates

Problem statement

In this, you have to generate the random dates using the JS function. For that, you have to create an Html file with the JS function as abc.html create a JS function that will return the random dates generated by the function and return output on the console.

Solution 

  • Create HTML file
  • Use function function randomDate(start, end)
  • Run HTML file in console

Js code to generate random dates

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Random date geneator</title>
</head>
<body>
<script>
    function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

console.log(randomDate(new Date(2012, 0, 1), new Date()));
</script>
</body>
</html>

Output

 

Js code to generate random decimal values

Problem statement

In this, you have to generate random decimal values using the JS function. For that, you have to create an Html file as xyz.html JS function that will return the generated random decimal values on the output screen.

Solution

  • Create HTML file
  • Use var x = Math.random()*10;
  • Run HTML file

Js code to generate random decimal values

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Generate random decimal number</title>
</head>
<body>
<script>
var x = Math.random()*10;

console.log(x);
</script>
</body>
</html>

Output