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

(새 문서: 분류: 랜덤 ==개요== ;함수 rand_choice() ==JavaScript== 분류: JavaScript {{참고|JavaScript rand_choice()}} <syntaxhighlight lang='javascript' run> const fruits = [...)
 
 
(같은 사용자의 중간 판 2개는 보이지 않습니다)
2번째 줄: 2번째 줄:
==개요==
==개요==
;함수 rand_choice()
;함수 rand_choice()
==Go==
[[분류: Go]]
{{참고|Go rand_choice}}
<syntaxhighlight lang='go' run>
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
fruits := []string{"apple", "banana", "melon"}
rand.Seed(time.Now().Unix())
f := fruits[rand.Intn(len(fruits))]
fmt.Println(f)
}
</syntaxhighlight>


==JavaScript==
==JavaScript==
10번째 줄: 31번째 줄:
const f = fruits[Math.floor(Math.random() * fruits.length)];
const f = fruits[Math.floor(Math.random() * fruits.length)];
console.log(f);
console.log(f);
</syntaxhighlight>
==Lua==
[[분류: Lua]]
{{참고|Lua rand_choice()}}
<syntaxhighlight lang='lua' run>
local fruits = { 'apple', 'banana', 'lemon', 'melon' }
print( fruits[ math.random( #fruits ) ] )
</syntaxhighlight>
==PHP==
[[분류: PHP]]
{{참고|PHP rand_choice()}}
<syntaxhighlight lang='php' run>
<?php
$fruits = ['apple', 'banana', 'melon'];
$f = $fruits[array_rand($fruits, 1)];
var_dump($f);
</syntaxhighlight>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 rand_between()]]
* [[함수 rand_between()]]

2023년 3월 9일 (목) 13:02 기준 최신판

1 개요[ | ]

함수 rand_choice()

2 Go[ | ]

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	fruits := []string{"apple", "banana", "melon"}

	rand.Seed(time.Now().Unix())
	f := fruits[rand.Intn(len(fruits))]
	fmt.Println(f)
}

3 JavaScript[ | ]

const fruits = ['apple', 'banana', 'melon'];
const f = fruits[Math.floor(Math.random() * fruits.length)];
console.log(f);

4 Lua[ | ]

local fruits = { 'apple', 'banana', 'lemon', 'melon' }
print( fruits[ math.random( #fruits ) ] )

5 PHP[ | ]

<?php
$fruits = ['apple', 'banana', 'melon'];
$f = $fruits[array_rand($fruits, 1)];
var_dump($f);

6 같이 보기[ | ]

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