(새 문서: category: 수학 {{lowercase title}} ;rand_between *rand_between(1,6) → 1 or 2 or 3 or 4 or 5 or 6 (Uniform distribution) ==Bash== category: bash <source lang='bash'> MIN=1...) |
|||
40번째 줄: | 40번째 줄: | ||
</source> | </source> | ||
== | ==JavaScript== | ||
[[category: | [[category: JavaScript]] | ||
<source lang='javascript'> | <source lang='javascript'> | ||
function rand_between(min, max) { | function rand_between(min, max) { |
2014년 5월 2일 (금) 22:49 판
- rand_between
- rand_between(1,6) → 1 or 2 or 3 or 4 or 5 or 6 (Uniform distribution)
1 Bash
Bash
Copy
MIN=1
MAX=6
R=$(($MIN+RANDOM%($MAX-$MIN+1)))
echo $R
# 1-6
Bash
Copy
function rand_between() {
echo $(($1+RANDOM%($2-$1+1)))
}
R=`rand_between 1 6`
echo $R
# 1-6
2 Excel
PHP
Copy
=RANDBETWEEN(1,6)
3 Java
Java
Copy
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
JavaScript
Copy
function rand_between(min, max) {
return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
}
5 Objective-C
Objective-C
Copy
- (int)rand_between:(int)min and:(int)max {
return min + arc4random() % (max - min + 1)
}
6 Perl
Perl
Copy
sub rand_between {
my ($min, $max) = @_;
return $min + int(rand($max-$min+1));
}
7 PHP
PHP
Copy
$r = mt_rand(1,6);
PHP
Copy
$r = rand(1,6);
8 Python
Python
Copy
import random
r = random.randint(1,6)
9 Ruby
Ruby
Copy
def rand_between(min, max)
min + rand(max-min+1)
end
10 SQL
10.1 MySQL
bat
Copy
SELECT ROUND( RAND() * (10-6) +6 )
11 See also
편집자 Jmnote Jmnote bot Ykhwong
로그인하시면 댓글을 쓸 수 있습니다.