본문 바로가기
개념/혼자 공부하는 Javascript

method) Math.random() method

by kiseno 2025. 1. 16.
728x90
반응형
SMALL
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        const num = Math.random()

        console.log('random number')
        console.log('0~10 random number', num * 10)
        console.log('0~50 random number', num * 50)
        console.log('')

        console.log('random range number')
        console.log('-5~5 : ', num * 10 - 5)
        console.log('-25~25 : ', num * 50 -25)
        console.log('')

        console.log('random integer number')
        console.log('-5~5 : ',Math.floor(num * 10 - 5))
        console.log('-25~25 : ',Math.floor(num * 50 -25))
    </script>
</head>
<body>

</body>
</html>

### Generating a Base Random Number
- `const num = Math.random()` generates a base random floating-point number between 0 (inclusive) and 1 (exclusive).

### Scaling the Random Number
- The script first demonstrates how to scale this base number to different ranges:
  - Multiplying `num` by 10 to get a range of 0 to just under 10.
  - Multiplying `num` by 50 to get a range of 0 to just under 50.

### Adjusting the Range
- Next, the script shows how to adjust these scaled numbers to different ranges that include negative values:
  - By subtracting 5 from `num * 10`, the range is shifted to -5 to just under 5.
  - By subtracting 25 from `num * 50`, the range is shifted to -25 to just under 25.

### Generating Integer Values
- Finally, the script demonstrates converting these adjusted numbers into integers using `Math.floor`, which rounds down to the nearest whole number:
  - `Math.floor(num * 10 - 5)` results in integer values from -5 to 4 (because the maximum value, just under 5, is floored to 4).
  - `Math.floor(num * 50 -25)` results in integer values from -25 to 24 (because the maximum value, just under 25, is floored to 24).

### Understanding the Output
The script logs all these calculations to the console, showing the versatility of `Math.random` for generating random numbers in JavaScript:

- **Random floating-point ranges**: It first prints out the base random number scaled to 0-10 and 0-50 ranges.
- **Random floating-point numbers within negative to positive ranges**: It shows adjusted ranges, such as -5 to 5 and -25 to 25, still as floating-point numbers.
- **Random integer ranges**: It finally demonstrates generating integers within specified ranges, including negative values, by applying `Math.floor` to the previously adjusted numbers.

This approach of manipulating the output of `Math.random()` is useful for a wide variety of applications where random numbers within specific ranges are needed, including games, simulations, and randomized data generation.

728x90
반응형
LIST

'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글

method) Lodash_sortBy() method  (1) 2025.01.18
method) map() method  (0) 2025.01.17
method) prototype method  (1) 2025.01.15
method) querySelector()  (0) 2025.01.14
method) querySelectorAll() method  (0) 2025.01.13