Wednesday, April 23, 2025

JavaScript Random Numbers

Random numbers are useful in programming whenever you need to generate unpredictable or random events or data. They can be used to add variability and unpredictability to your applications, as well as to test the robustness of your code. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	//One random number:
	document.write(Math.random() + "<br>");
	
	//Whole Number Generator
	var random_num = (Math.random() * 10);
	var whole_num = Math.round(random_num);
	
	document.write(whole_num);
	
</script>
	
</body>
</html>

This script generates random numbers using the Math.random() method.

The first line of code generates a random number between 0 and 1 using the Math.random() method and outputs it to the webpage using the document.write() method, followed by a line break using the <br> tag.

The second part of the script generates a random whole number between 0 and 10 using the Math.random() method. First, a random number between 0 and 1 is generated using Math.random(), then it is multiplied by 10 to get a random number between 0 and 10. The resulting number is not guaranteed to be a whole number, so the Math.round() method is used to round the number to the nearest integer. Finally, the resulting whole number is output to the webpage using the document.write() method.

When the script is run, it outputs a random number between 0 and 1 and a random whole number between 0 and 10 to the webpage. The Math.random() method is useful for generating unpredictable and random data in JavaScript applications. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	//Generate 10 Whole Random Number
	for (x = 0; x < 10; x = x + 1) {
		document.write(Math.round(Math.random() * 10) + "<br>");
	}
	
</script>
	
</body>
</html>

This script generates 10 random whole numbers between 0 and 10 using a for loop and the Math.random() and Math.round() methods.

Inside the loop, the Math.random() method is used to generate a random number between 0 and 1, which is then multiplied by 10 to get a random number between 0 and 10. The resulting number is not guaranteed to be a whole number, so the Math.round() method is used to round the number to the nearest integer. Finally, each resulting whole number is output to the webpage using the document.write() method, followed by a line break using the <br> tag.

When the script is run, it generates 10 random whole numbers between 0 and 10 and outputs them to the webpage. This script can be useful for generating a random set of data for testing or simulation purposes.

Many programming languages have similar functionality to JavaScript's Math module and random() method for generating random numbers.

For example, Python has a random module which provides functions for generating random numbers, including random() which returns a random floating-point number between 0 and 1. The module also includes functions for generating random integers within a range, shuffling lists, and more.

Similarly, Java has a java.util.Random class which can be used to generate random numbers. The class has methods for generating random integers, floating-point numbers, and more.

Other programming languages such as C++, C#, Ruby, and PHP also provide functionality for generating random numbers and performing mathematical operations.

While the exact syntax and available methods may differ between programming languages, the concepts and functionality are similar across many languages.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...