본문 바로가기
Computer Science/Javascript

[Javascript ] 자바스크립트 랜덤 숫자, Math Random() 함수

by 기억보다 기록을 2023. 4. 2.
반응형

JavaScript 


  • JavaScript는 웨페이지에서 복잡한 기능을 구현할 수 있도록 하는 객제현 스크린팅 프로그램밍 언어입니다.
  • 자바스크립트는 동적이며, 타입을 명시할 필요가 없는 인터프리터 언어입니다.
  • 자바스크립트는 객체 지향형 프로그래밍과 함수형 프로그래밍을 모두 표현할 수 있습니다.
  • Interpreted Language as JavaScript, Python Ruby
  • Compiled Languages: Java, C/C++, Swift

 

 

Math Random() 함수


  • Static method returns a floating-point, random number between 0 and 1.
  • Getting a random number between two numbers
 function getRandomNumbers(startPoint, endPoint) {
        return Math.random() * (endPoint - startPoint) + startPoint; 
     }

getRandomNumbers(5,200);

 

  • Getting a random integer between two values
function getRandomInteger(startPoint, endPoint) {
       
    startPoint = Math.ceil(startPoint); 
    endPoint = Math.ceil(endPoint);
    return Math.random() * (endPoint - startPoint) + startPoint; 
}

 getRandomInteger(5,200)

 

  • Math.Random() + Math.floor() => only integer random numbers
function getRandomInteger(startPoint, endPoint) {
       
    startPoint = Math.ceil(startPoint); 
    endPoint = Math.ceil(endPoint);
    
    return Math.floor(Math.random() * (endPoint - startPoint) + startPoint); 
}

 getRandomInteger(5,200)

 

 

 

Comparators and Equality,  Strict equality 

재미있는 조건문 연산자 맛보기

  • Equality " == " : operator checks whether its two operands are equal, returning a Boolean value.
  • Object: return " true " only if its two operands are same abject
  • String: return " true " only if two operands have same characters with same order
  • Boolean : return " true " only if two operands both true or false
  • BigInt: return true only if both operands have the same value
  • Symbol: return true only if both operands reference the same symbol
  • If one of operands is null or undefined, other must be null or undefined for returned true
  • Super Interesting Samples  : 
1 == 1,  true
'hello' == 'hello',  true
'1' == 1, true
0 == false,  true
+0 == -0 , true
Nan == Nan, is false
null == null, true
null == undefined, true
undefined == undefined, true

 

  • Strict Equality " === " : operator checks whether the two operands are equal, returning a Boolean value.
  • Difference of equality between Strict Equality.  Most notable difference of equality vs strict equality is that if the operands are different types the == operator attempts to convert them to the same type before comparing.
         
  • Unlike the equality "==", the strict equality operator always consider operands of different types to be different.
  • Equality vs Strict Equality Samples:
+0 === -0, returns true

1 == 1 vs 1 === 1 
//also returns true

'hello' == 'hello' 'hello' === 'hello'
// both true

'1' == 1 is true, but '1' === 1 returns false

0 == false is true,  but '0' === 'false' is exactly false

 

  • if both operations are null or undefined, returns true
null == undefined is true,  but null === undefined is false

 

  • If either operands is NaN, returns false.
NaN == NaN, NaN === Nan, returns false

 

 


반응형