💠프로그래밍 언어/JavaScript
[심화] random 함수 사용법 알기
튤
2024. 7. 10. 22:35
728x90
random( )
random( )
⸰ 0부터 1 미만까지 무작위로 부동소수점의 난수를 추출
Math.random() * (최댓값 - 최솟값) + 최솟값;
⸰ (최댓값 - 최솟값) : 원하는 범위의 숫자 개수
⸰ + 최솟값 : 시작하는 숫자
⸰ 자연수로 바꿔주려면,
Math.floor(Math.random() * (최댓값 - 최솟값) + 최솟값);
Math.floor(Math.random( )) | 0 <= x < 1 | 0, 1 |
Math.floor(Math.random( ) * (10 - 1) + 1) | 1 <= x <= 10 | 1 ~ 10 |
Math.floor(Math.random( ) * (10 - 3) + 1) | 3 <= x <= 10 | 3 ~ 10 |
728x90