(새 문서: 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...) |
|||
(사용자 3명의 중간 판 20개는 보이지 않습니다) | |||
2번째 줄: | 2번째 줄: | ||
{{lowercase title}} | {{lowercase title}} | ||
;rand_between | ;rand_between | ||
;randint | |||
*rand_between(1,6) → 1 or 2 or 3 or 4 or 5 or 6 (Uniform distribution) | *rand_between(1,6) → 1 or 2 or 3 or 4 or 5 or 6 (Uniform distribution) | ||
==Bash== | ==Bash== | ||
[[category: bash]] | [[category: bash]] | ||
< | <syntaxhighlight lang='bash'> | ||
MIN=1 | MIN=1 | ||
MAX=6 | MAX=6 | ||
12번째 줄: | 13번째 줄: | ||
echo $R | echo $R | ||
# 1-6 | # 1-6 | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang='bash'> | ||
function rand_between() { | function rand_between() { | ||
echo $(($1+RANDOM%($2-$1+1))) | echo $(($1+RANDOM%($2-$1+1))) | ||
20번째 줄: | 21번째 줄: | ||
echo $R | echo $R | ||
# 1-6 | # 1-6 | ||
</ | </syntaxhighlight> | ||
==Excel== | ==Excel== | ||
[[category: Excel]] | [[category: Excel]] | ||
< | <syntaxhighlight lang='php'> | ||
=RANDBETWEEN(1,6) | =RANDBETWEEN(1,6) | ||
</ | </syntaxhighlight> | ||
==Go== | |||
[[분류: Go]] | |||
{{참고|Go randBetween()}} | |||
<syntaxhighlight lang='go'> | |||
package main | |||
import ( | |||
"fmt" | |||
"math/rand" | |||
) | |||
func randBetween(min, max int) int { | |||
return rand.Intn(max-min+1) + min | |||
} | |||
func main() { | |||
fmt.Println(randBetween(1, 6)) | |||
} | |||
</syntaxhighlight> | |||
==Java== | ==Java== | ||
[[category: Java]] | [[category: Java]] | ||
< | <syntaxhighlight lang='java'> | ||
import java.util.Random; | import java.util.Random; | ||
38번째 줄: | 59번째 줄: | ||
return min + rng.nextInt(max-min+1); | return min + rng.nextInt(max-min+1); | ||
} | } | ||
</ | </syntaxhighlight> | ||
== | ==JavaScript== | ||
[[category: | [[category: 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; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<syntaxhighlight lang='JavaScript'> | |||
var r = Math.floor(Math.random() * 3); | |||
// range 0~2 | |||
</syntaxhighlight> | |||
==Lua== | |||
[[분류: Lua]] | |||
{{참고|lua math.random()}} | |||
<syntaxhighlight lang='lua' run> | |||
local r = math.random(1, 6) | |||
print(r) -- 1-6 | |||
</syntaxhighlight> | |||
==Objective-C== | ==Objective-C== | ||
[[category: Objective-C]] | [[category: Objective-C]] | ||
< | <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) | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Perl== | ==Perl== | ||
[[category: Perl]] | [[category: 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)); | ||
} | } | ||
</ | </syntaxhighlight> | ||
==PHP== | ==PHP== | ||
[[category: PHP]] | [[category: PHP]] | ||
< | {{참고|PHP mt_rand()}} | ||
{{참고|PHP rand()}} | |||
<syntaxhighlight lang='php'> | |||
$r = mt_rand(1,6); | $r = mt_rand(1,6); | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang='php'> | ||
$r = rand(1,6); | $r = rand(1,6); | ||
</ | </syntaxhighlight> | ||
==Python== | ==Python== | ||
[[category: Python]] | [[category: Python]] | ||
< | <syntaxhighlight lang='python'> | ||
import random | import random | ||
r = random.randint(1,6) | r = random.randint(1,6) | ||
</ | </syntaxhighlight> | ||
<syntaxhighlight lang='python'> | |||
from random import randint | |||
r = randint(1,6) | |||
</syntaxhighlight> | |||
==Ruby== | ==Ruby== | ||
[[category: Ruby]] | [[category: 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 | ||
</ | </syntaxhighlight> | ||
==Windows Batch== | |||
<syntaxhighlight lang='batch'> | |||
@echo off | |||
SET MIN=1 | |||
SET MAX=6 | |||
SET /A R=%RANDOM% * %MAX% / 32768 + %MIN% | |||
echo %R% | |||
REM 1-6 | |||
</syntaxhighlight> | |||
==SQL== | ==SQL== | ||
93번째 줄: | 142번째 줄: | ||
===MySQL=== | ===MySQL=== | ||
[[category: MySQL]] | [[category: MySQL]] | ||
< | <syntaxhighlight lang='mysql'> | ||
SELECT ROUND( RAND() * (10-6) +6 ) | SELECT ROUND( RAND() * (10-6) +6 ) | ||
</ | </syntaxhighlight> | ||
==같이 보기== | |||
* [[rand]] | |||
* [[shuffle]] | |||
* [[함수 rand_choice()]] | |||
== | ==참고== | ||
* | *http://eqcode.com/wiki/Rand_between | ||
2023년 3월 9일 (목) 12:45 기준 최신판
- rand_between
- randint
- 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 Go[ | ]
Go randBetween() 문서를 참고하십시오.
Go
Copy
package main
import (
"fmt"
"math/rand"
)
func randBetween(min, max int) int {
return rand.Intn(max-min+1) + min
}
func main() {
fmt.Println(randBetween(1, 6))
}
4 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);
}
5 JavaScript[ | ]
JavaScript
Copy
function rand_between(min, max) {
return min + Math.ceil( (max-min+1) * Math.random() ) - 1;
}
JavaScript
Copy
var r = Math.floor(Math.random() * 3);
// range 0~2
6 Lua[ | ]
lua math.random() 문서를 참고하십시오.
lua
Copy
local r = math.random(1, 6)
print(r) -- 1-6
Loading
7 Objective-C[ | ]
Objective-C
Copy
- (int)rand_between:(int)min and:(int)max {
return min + arc4random() % (max - min + 1)
}
8 Perl[ | ]
Perl
Copy
sub rand_between {
my ($min, $max) = @_;
return $min + int(rand($max-$min+1));
}
9 PHP[ | ]
PHP mt_rand() 문서를 참고하십시오.
PHP rand() 문서를 참고하십시오.
PHP
Copy
$r = mt_rand(1,6);
PHP
Copy
$r = rand(1,6);
10 Python[ | ]
Python
Copy
import random
r = random.randint(1,6)
Python
Copy
from random import randint
r = randint(1,6)
11 Ruby[ | ]
Ruby
Copy
def rand_between(min, max)
min + rand(max-min+1)
end
12 Windows Batch[ | ]
batch
Copy
@echo off
SET MIN=1
SET MAX=6
SET /A R=%RANDOM% * %MAX% / 32768 + %MIN%
echo %R%
REM 1-6
13 SQL[ | ]
13.1 MySQL[ | ]
MySQL
Copy
SELECT ROUND( RAND() * (10-6) +6 )