// Collect all the possible outputs.
String[] outputs = new String[] { "output 1", "output 2", "output 3" };
// Use the core library class java.util.Random to get a source of pseudo-randomness.
Random random = new Random();
// Pick one and print it.
System.out.println(outputs[random.nextInt(outputs.length)]);
Run Code Online (Sandbox Code Playgroud)
nextInt 返回一个均匀分布在0和其参数-1之间的伪随机数.
由于Random是伪随机的,如果您需要输出即使是观察了许多随机选择的人也无法使用,请SecureRandom改用.
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |