함수 rand choice()

1 개요[ | ]

함수 rand_choice()

2 Go[ | ]

Go
CPU
-1.0s
MEM
-0M
-1.0s
Copy
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)
}
banana

3 JavaScript[ | ]

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

4 Lua[ | ]

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

5 PHP[ | ]

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

6 같이 보기[ | ]