Math.random()
함수는 0.0000000000000000에서 0.9999999999999999 사이 값에서만 return해준다.
조금 손을 봐서 내게 필요한 랜덤값으로 만들어
활용해보겠다.
Math.random() * 10
#1≤리턴값<10
Math.random(10 - 1) + 1
#1≤리턴값≤10
Math.random(10 - 1 + 1) + 1
Math.floor( 45.05); // 45
Math.floor( 4 ); // 4
Math.floor(-45.05); // -46
Math.ceil( 45.05); // 46
Math.ceil( 4 ); // 4
Math.ceil(-45.05); // -45
Math.round( 3.5); // 4
Math.round( 3.45); // 3
Assignment
앞으로 랜덤함수를 쓸 일이 정말 많습니다.
그런데 Math.random()으로는 내가 원하는 범위의 랜덤수를 얻을 수가 없습니다.
항상 0.0000000000000000에서 0.9999999999999999 사이 값에서만 return해주기 때문이죠.
최소(min), 최대값(max)을 받아 그 사이의 랜덤수를 return하는 함수를 구현해주세요.function getRandomNumber (min, max) { // return 랜덤수; var random = Math.random(); result = Math.floor( random*(max-min+1)) +min; console.log(result); return result; }