JAVA8 从数组中随机一个元素
Java8 从数组中随机一个元素
import java.util.Random; @Test public void randomList() { String[] li = {"中国","美国","英国","法国","德国","俄罗斯"}; Random random = new Random(); for (int j = 0; j < 5; j++) { String country = li[random.nextInt(li.length)]; System.out.println("country = " + country); } }
output =>
country = 法国
country = 中国
country = 俄罗斯
country = 中国
country = 中国
python 从列表中随机一个元素
li = ["中国", "美国", "英国", "法国", "德国", "俄罗斯"] import random for i in range(5): print(random.choice(li))
output =>
中国
英国
俄罗斯
俄罗斯
俄罗斯