• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Math] JS - Math.random() 메서드 ★ - 랜덤값 (= 임의값 = Math.random메서드 = 매스랜덤메서드) ※ 지정범위 랜덤숫자 ※ 배열순서랜덤

목차
  1. Math.random() 예제 - (0 ≤ x<1) 랜덤 부동소수
  2. Math.random() 정의
  3. Math.random() 구문
  4. Math.random() 예제 - (1 ≤ x ≤ 10) 랜덤 정수
  5. Math.random() 예제 - (1 ≤ x ≤ 100) 랜덤 정수
  6. Math.random() 예제 - (min ≤ X < max) 함수
  7. Math.random() 예제 - (min ≤ X ≤ max) 함수
  8. Math.random() 예제 - 배열 순서 랜덤 적용

 

Math.random() 예제 - (0 ≤ x<1) 랜덤 부동소수 

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

function homzzang() {

  var x =  Math.random();

  document.getElementById("demo").innerHTML = x;

}

</script>

 

결과보기

 

Math.random() 정의

 

0 ≤ x < 1 범위의 부동 소수 형태의 의사 난수 (= 랜덤 숫자) 반환. 

(즉, 최소값은 포함 O , 최대값은 포함 x)

 


 

1.

암호학 상 안전한 난수 생성 X .

따라서, 보안 코드로 사용 금지.

 

2.

랜덤 정수 표현 위해서 Math.floor() 객체와 주로 함게 사용. 

  • Math.floor(Math.random() * 10); // 0 ~ 9
  • Math.floor(Math.random() * 11); // 0 ~ 10
  • Math.floor(Math.random() * 100); // 0 ~ 99
  • Math.floor(Math.random() * 101); // 0 ~ 100
  • Math.floor(Math.random() * 10) + 1; // 1 ~ 10
  • Math.floor(Math.random() * 100) + 1; // 1 ~ 100

 

3.
ECMAScript 1

4.
모든 주요브라우저 지원.

 

 

Math.random() 구문

 

Math.random()

 


[매개변수]

 

없음.

 


[반환값]

 

(0 ≤ n < 1) 범위의 랜덤 숫자 반환.

 

 

Math.random() 예제 - (1 ≤ x ≤ 10) 랜덤 정수

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

function homzzang() {

  var x = Math.floor((Math.random() * 10) + 1);

  document.getElementById("demo").innerHTML = x;

}

</script>

 

결과보기


Math.random() 예제 - (1 ≤ x ≤ 100) 랜덤 정수

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

function homzzang() {

  var x = document.getElementById("demo")

  x.innerHTML = Math.floor((Math.random() * 100) + 1);

}

</script>

 

결과보기

 

Math.random() 예제 - (min ≤ X < max) 함수

 

<button onclick="document.getElementById('demo').innerHTML = homzzang(0,10)">클릭</button>


<p id="demo"></p>


<script>

function homzzang(min, max) {

  return Math.floor(Math.random() * (max - min)) + min;

}

</script>

 

결과보기 


[예제] - (0 ≤ n < 10) 랜덤 숫자

 

<button onclick="hz(0, 10)">클릭</button>


<p id="demo"></p>


<script>

function hz(min,max) {

  document.getElementById('demo').innerHTML = homzzang(min, max);

}

function homzzang(min, max) {

  return Math.floor(Math.random() * (max - min)) + min; 

}

</script>

 

결과보기

※ 함수 정의 순서는 상관 없음.

 

Math.random() 예제 - (min ≤ X ≤ max) 함수

 

<button onclick="document.getElementById('demo').innerHTML = homzzang(0,10)">클릭</button>


<p id="demo"></p>


<script>

function homzzang(min, max) {

  return Math.floor(Math.random() * (max - min + 1)) + min;

}

</script>

 

결과보기


[예제] - (0 ≤ n ≤ 10) 랜덤 숫자

 

<button onclick="hz(0, 10)">클릭</button>


<p id="demo"></p>


<script>

function hz(min,max) {

  document.getElementById('demo').innerHTML = homzzang(min, max);

}

function homzzang(min, max) {

  return Math.floor(Math.random() * (max - min + 1)) + min; 

}

</script>

 

결과보기

※ 함수 정의 순서는 상관 없음.

 

Math.random() 예제 - 배열 순서 랜덤 적용

※ JS엔 shuffle() 함수 존재 X (∴ 아래 사용자정의함수 필요.)

 

function shuffle(array) {

    array.sort(() => Math.random() - 0.5);

var nums = [1, 2, 3];

shuffle(nums);

console.log(nums);

 

강한남v 님 (220217) https://sir.kr/qa/449765

방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Math JS - Math.min() 메서드 ★ - 최소값 (= 최솟값 = Math.min메서드 = 매스민메서드)
Math JS - Math.pow() 메서드 ★ - 승수 (= 거듭제곱 = pow메서드 = 매스포우메서드/매스파워메서…
Math JS - Math.random() 메서드 ★ - 랜덤값 (= 임의값 = Math.random메서드 = 매스랜…
Math JS - Math.round() 메서드 ★ - 일반반올림 (= 보통반올림 = Math.round메서드 매스라…
Math JS - Math.sin() 메서드 - 사인값
Math JS - Math.sqrt() 메서드 - 제곱근
Math JS - Math.tan() 메서드 - 탄젠트값
Date JS - Date 객체 - 날짜객체 (속성 + 메서드) 종류 ※Date()함수 = Date함수 = 데이트 함…
Date JS - constructor 속성 - 객체생성자함수 (날짜 경우)
Date JS - prototype 속성(날짜) - 사용자정의 속성/메서드 추가 (날짜객체경우 = 프로토타입)
Date JS - getDate() 메서드 ★ - 일자 얻기 (= getDate메서드 = 겟데이트)
Date JS - getDay() 메서드 - 요일 얻기 (예: 4) (= 요일 추출 = 요일 표시 = getDay메서…
Date JS - getFullYear() 메서드 - 년도 (= 4자리 전체년도얻기 = 겟풀이어)
Date JS - getHours() 메서드 - 시 (= 시얻기 = 시간얻기)
Date JS - getMilliseconds() 메서드 - 밀리초 (= 밀리세컨드)
Date JS - getMinutes() 메서드 - 분 (= 분얻기)
Date JS - getMonth() 메서드 - 월얻기 (= 달얻기 = 달월얻기 = getMonth메서드 = 겟먼스메…
Date JS - getSeconds() 메서드 - 초 얻기 (= 겟세컨즈메서드)
Date JS - getTime() 메서드 ★ - 시간 얻기 (= 타임스탬프얻기 = getTime메서드 = 겟타임메서…
Date JS - getTimezoneOffset() 메서드 - UTC시간과의 시차
11/67
목록
찾아주셔서 감사합니다. Since 2012