🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the Math.random() method in TypeScript. This method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive). Understanding how to use Math.random() is useful for generating random numbers for various purposes such as simulations, games, and random selections.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The Math.random() method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive). This method is commonly used for generating random numbers.
2. Syntax
Math.random();
Parameters
The Math.random() method does not take any parameters.
Return Value
The method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
3. Examples
Let's look at some examples to understand how Math.random() works in TypeScript.
Example 1: Basic Usage
In this example, we use Math.random() to generate a random number between 0 and 1.
let randomNumber = Math.random();
console.log(randomNumber); // Output: A random number between 0 and 1, e.g., 0.763474292443475
Example 2: Generating a Random Number Between 0 and 10
In this example, we use Math.random() to generate a random number between 0 and 10.
let randomNumber = Math.random() * 10;
console.log(randomNumber); // Output: A random number between 0 and 10, e.g., 7.264328374738
Example 3: Generating an Integer Between 0 and 10
In this example, we use Math.random() in combination with Math.floor() to generate a random integer between 0 and 10.
let randomNumber = Math.floor(Math.random() * 11);
console.log(randomNumber); // Output: A random integer between 0 and 10, e.g., 3
Example 4: Generating a Random Integer Between Two Values
In this example, we create a function that uses Math.random() to generate a random integer between two specified values, inclusive of the minimum value and exclusive of the maximum value.
function getRandomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min) + min);
}
let randomNumber = getRandomInt(5, 15);
console.log(randomNumber); // Output: A random integer between 5 and 15, e.g., 9
Example 5: Generating a Random Floating-Point Number Between Two Values
In this example, we create a function that uses Math.random() to generate a random floating-point number between two specified values.
function getRandomFloat(min: number, max: number): number {
return Math.random() * (max - min) + min;
}
let randomNumber = getRandomFloat(1.5, 5.5);
console.log(randomNumber); // Output: A random floating-point number between 1.5 and 5.5, e.g., 3.781723647
Example 6: Simulating a Dice Roll
In this example, we use Math.random() to simulate a dice roll, generating a random integer between 1 and 6.
function rollDice(): number {
return Math.floor(Math.random() * 6) + 1;
}
let diceRoll = rollDice();
console.log(diceRoll); // Output: A random integer between 1 and 6, e.g., 4
4. Conclusion
In this chapter, we explored the Math.random() method in TypeScript, which is used to generate a pseudo-random number between 0 (inclusive) and 1 (exclusive). We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Math.random() effectively can help in various applications where random numbers are required, such as simulations, games, and random selections.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment