"함수 rand between()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
7번째 줄: 7번째 줄:
==Bash==
==Bash==
[[category: bash]]
[[category: bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
MIN=1
MIN=1
MAX=6
MAX=6
13번째 줄: 13번째 줄:
echo $R
echo $R
# 1-6
# 1-6
</source>
</syntaxhighlight>
<source lang='bash'>
<syntaxhighlight lang='bash'>
function rand_between() {
function rand_between() {
   echo $(($1+RANDOM%($2-$1+1)))
   echo $(($1+RANDOM%($2-$1+1)))
21번째 줄: 21번째 줄:
echo $R
echo $R
# 1-6
# 1-6
</source>
</syntaxhighlight>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
<source lang='php'>
<syntaxhighlight lang='php'>
=RANDBETWEEN(1,6)
=RANDBETWEEN(1,6)
</source>
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
import java.util.Random;
import java.util.Random;


39번째 줄: 39번째 줄:
   return min + rng.nextInt(max-min+1);
   return min + rng.nextInt(max-min+1);
}
}
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
function rand_between(min, max) {
function rand_between(min, max) {
   return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
   return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
}
}
</source>
</syntaxhighlight>
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
var r = Math.floor(Math.random() * 3);
var r = Math.floor(Math.random() * 3);
// range 0~2
// range 0~2
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
- (int)rand_between:(int)min and:(int)max {
- (int)rand_between:(int)min and:(int)max {
   return min + arc4random() % (max - min + 1)
   return min + arc4random() % (max - min + 1)
}
}
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl'>
sub rand_between {
sub rand_between {
   my ($min, $max) = @_;
   my ($min, $max) = @_;
   return $min + int(rand($max-$min+1));
   return $min + int(rand($max-$min+1));
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
74번째 줄: 74번째 줄:
{{참고|PHP mt_rand()}}
{{참고|PHP mt_rand()}}
{{참고|PHP rand()}}
{{참고|PHP rand()}}
<source lang='php'>
<syntaxhighlight lang='php'>
$r = mt_rand(1,6);
$r = mt_rand(1,6);
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
$r = rand(1,6);
$r = rand(1,6);
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='python'>
<syntaxhighlight lang='python'>
import random
import random
r = random.randint(1,6)
r = random.randint(1,6)
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python'>
from random import randint
from random import randint
r = randint(1,6)
r = randint(1,6)
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='ruby'>
<syntaxhighlight lang='ruby'>
def rand_between(min, max)
def rand_between(min, max)
   min + rand(max-min+1)
   min + rand(max-min+1)
end
end
</source>
</syntaxhighlight>


==Windows Batch==
==Windows Batch==
<source lang='batch'>
<syntaxhighlight lang='batch'>
@echo off
@echo off
SET MIN=1
SET MIN=1
108번째 줄: 108번째 줄:
echo %R%
echo %R%
REM 1-6
REM 1-6
</source>
</syntaxhighlight>


==SQL==
==SQL==
114번째 줄: 114번째 줄:
===MySQL===
===MySQL===
[[category: MySQL]]
[[category: MySQL]]
<source lang='mysql'>
<syntaxhighlight lang='mysql'>
SELECT ROUND( RAND() * (10-6) +6 )
SELECT ROUND( RAND() * (10-6) +6 )
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:33 판

rand_between
randint
  • rand_between(1,6) → 1 or 2 or 3 or 4 or 5 or 6 (Uniform distribution)

1 Bash

MIN=1
MAX=6
R=$(($MIN+RANDOM%($MAX-$MIN+1)))
echo $R
# 1-6
function rand_between() {
  echo $(($1+RANDOM%($2-$1+1)))
}
R=`rand_between 1 6`
echo $R
# 1-6

2 Excel

=RANDBETWEEN(1,6)

3 Java

import java.util.Random;

private static final Random rng = new Random();

public static int randBetween(int min, int max) {
  return min + rng.nextInt(max-min+1);
}

4 JavaScript

function rand_between(min, max) {
  return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
}
var r = Math.floor(Math.random() * 3);
// range 0~2

5 Objective-C

- (int)rand_between:(int)min and:(int)max {
  return min + arc4random() % (max - min + 1)
}

6 Perl

sub rand_between {
  my ($min, $max) = @_;
  return $min + int(rand($max-$min+1));
}

7 PHP

$r = mt_rand(1,6);
$r = rand(1,6);

8 Python

import random
r = random.randint(1,6)
from random import randint
r = randint(1,6)

9 Ruby

def rand_between(min, max)
  min + rand(max-min+1)
end

10 Windows Batch

@echo off
SET MIN=1
SET MAX=6
SET /A R=%RANDOM% * %MAX% / 32768 + %MIN%
echo %R%
REM 1-6

11 SQL

11.1 MySQL

SELECT ROUND( RAND() * (10-6) +6 )

12 같이 보기

13 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}