Recent posts

Recent comments

Archive

Calender

«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Use the Google Translate, if you want to read this page in your language.
But its translation is probably inaccurate.
Go to the Google Translate.



[ Javascript ] Get real number at random.


random() 함수를 이용해서,
원하는 범위 안에서, 실수(real number) 하나를 랜덤으로 가져다주는 함수를 만들어보았다.

Using "Math.random()",
I wrote a function that returns one real number at random within the desired range.


 
<script type="text/javascript">
function getRandomNumber (){ 

    var decimal; 
    var random =  Math.random(); 
    var isFirstNumber =  function ( number  ){  return ( "" + random ).indexOf( number ) == 0; }; 

    switch ( arguments.length ){ 
        case 0 : 
                if ( isFirstNumber( 0.001 ) ){  decimal =  0;  } 
                else { 
                    var decimalDigits = ( "" + random ).replace( "0.", "" ).length; 
                    var exponential = Math.floor( Math.random() * decimalDigits ); 
                    decimal = ( exponential < 1 ) ? random : random * Math.pow( 10, exponential ); 
                } 

                break; 
        case 1 : 
                if ( isFirstNumber( 0.001 ) ){  decimal =  0;  } 
                else { 
                    var max = Number( arguments[ 0 ] ) || parseFloat( arguments[ 0 ] ); 

                    if ( isNaN( max ) || max === 0 || isFirstNumber( 0.999 ) ){  decimal =  max;  } 
                    else {  decimal = random * max;  } 
                } 

                break; 
        case 2 : 
                var temp, min, max; 

                temp =  ( arguments[0] < arguments[1] ) ? arguments[0] : arguments[1]; 
                min =  Number( temp ) || parseFloat( temp ); 

                temp =  ( arguments[0] < arguments[1] ) ? arguments[1] : arguments[0]; 
                max = Number( temp ) || parseFloat( temp ); 

                if ( isNaN( min ) || isNaN( max ) || min == max ){  decimal = ( min == max ) ? max : NaN;  } 
                else { 
                    if ( isFirstNumber( 0.001 ) ){  decimal =  min;  } 
                    else if ( isFirstNumber( 0.999 ) ){  decimal =  max;  } 
                    else {  decimal = ( random * (max - min) ) + min;  } 
                } 

                break; 
    } 

    if (  isNaN( decimal )  ){  return { real : NaN, integer : NaN, positive : NaN, negative : NaN };  } 

    if (  decimal == 0  ){  return { real : 0, integer : 0, positive : 0, negative : 0 };  } 

    return { real : decimal, 
                integer : decimal - ( decimal % 1 ), 
                positive : ( decimal < 0 ) ? -decimal : decimal, 
                negative : ( decimal > 0 ) ? -decimal : decimal 
               }; 
} 
</script>
 


아래는, 한정된 범위 없이 getRandomNumber() 함수를 실행했을 경우이다.

The following is a case of working the getRandomNumber() function without limited range.


 
<table>
  <tbody>
    <tr> <td>실수 ( real number ) </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>정수 ( integer ) </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>양수 ( positive number ) </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>음수 ( negative number ) </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber(); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
실수 ( real number )  
정수 ( integer )  
양수 ( positive number )  
음수 ( negative number )  


아래의 두 예문에서는, 0부터 14.09 사이의 실수(real number)를 가져온다.
최소값인 0과 최대값인 14.09도 나오기는 한다. 아주 가~~~끔...

The following two examples will return one real number between 0 and 14.09.
Very, very rarely...
it will return the minimum 0 or maximum 14.09.


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber( 14.09 ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber( " 14.09 " ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


아래 예문에서는, -14.09부터 0 사이의 실수(real number)를 가져온다.

The following returns one real number between -14.09 and 0.


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber( -14.09 ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


아래의 두 예문에서는, -3부터 5 사이의 실수(real number)를 가져온다.

The following two examples will return one real number between -3 and 5.


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber( -3, 5 ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">
function testing (){ 

    var random = getRandomNumber( 5, -3 ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


아래의 두 예문에서는, 결과가 모두 NaN으로 나올 것이다.

The following two examples will return NaN.


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">

function testing (){ 

    var random = getRandomNumber( undefined ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


 
<table>
  <tbody>
    <tr> <td>real number </td> <td id="demo1">&nbsp;</td> </tr>
    <tr> <td>integer </td> <td id="demo2">&nbsp;</td> </tr>
    <tr> <td>positive number </td> <td id="demo3">&nbsp;</td> </tr>
    <tr> <td>negative number </td> <td id="demo4">&nbsp;</td> </tr>
  </tbody>
</table>

<button onclick="testing()"> Click me </button>

<script type="text/javascript">

function testing (){ 

    var random = getRandomNumber( " abc 12 " ); 

    var a = random.real; 
    var b = random.integer; 
    var c = random.positive; 
    var d = random.negative; 

    demo1.innerHTML = a; 
    demo2.innerHTML = b; 
    demo3.innerHTML = c; 
    demo4.innerHTML = d; 
} 
</script>
 
real number  
integer  
positive number  
negative number  


이 내용이 도움이 되셨다면, 아래의 하트 버튼을 눌러주세요. *^^*
If this article is helpful to you, please press the heart button. *^^*