你如何生成特定的随机数?
How do you generate specific random number?
我需要 float
或 integer
,不是完全生成的,而是随机选择的。
我需要使用和导入 math.random class
吗?
例如我有 3 个整数:1、6 和 3。我希望随机选择其中一个。
根据您想要的数字生成一个随机索引,然后从 array
中取出数字
int []nums = {1,3,6};
int max = nums.length - 1;
int min = 0;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
System.out.println(nums[randomNum]);
创建一个允许的数字数组(即 new int[] {1, 6, 3}
),然后在 0..数组长度范围内随机选择一个 int
。在随机索引处选择值将产生您想要的结果。
假设你有一个数组(希望你知道 that 是什么)你可以做:
int choose = myNumbers[(int)Math.random() * myNumbers.length];
如果您想要三个值中的每一个,但顺序是随机的,那么您可以将它们放在一个数组中。并且,洗牌该数组,您可以使用 Arrays.asList(T...)
and Collections.shuffle(List)
. Next, iterate the values in the array (perhaps with a for-each
loop)。像
public static void main(String[] args) {
Integer[] arr = new Integer[] { 1, 6, 3 };
Collections.shuffle(Arrays.asList(arr));
for (int i : arr) {
System.out.println(i);
}
}
但是,如果您真的需要一次只选择一个随机值(比如 10 次),那么您可以使用 Random
and nextInt(int)
和 for
循环。这可能看起来像
public static void main(String[] args) {
int[] arr = new int[] { 1, 6, 3 };
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int index = rand.nextInt(arr.length);
System.out.println(arr[index]);
}
}
您必须创建一个数组,其中包含您想要随机获取的值。像这样:
int [] values = {1,3,6};
而且您永远不必忘记输入:
Random rand = new Random();
因为如果不放,随机函数将无法正常工作。
那么,现在你可以随机访问你的值了,就像这样:
int numObtained = rand.nextInt((values.length - 0) + 1) + 0;
最终代码为:
int [] values = {1,3,6};
Random rand = new Random();
int numObtained = rand.nextInt((values.length - 0) + 1) + 0;
希望对您有所帮助!
这可能会有所帮助。
int i[] = new int[]{3,4,6, 5, 8, 10, 14}; // given sets of array
int randomIndex = (int) (Math.random() * i.length); // generates a random index
System.out.println("random selected value " + i[randomIndex]);
我需要 float
或 integer
,不是完全生成的,而是随机选择的。
我需要使用和导入 math.random class
吗?
例如我有 3 个整数:1、6 和 3。我希望随机选择其中一个。
根据您想要的数字生成一个随机索引,然后从 array
int []nums = {1,3,6};
int max = nums.length - 1;
int min = 0;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
System.out.println(nums[randomNum]);
创建一个允许的数字数组(即 new int[] {1, 6, 3}
),然后在 0..数组长度范围内随机选择一个 int
。在随机索引处选择值将产生您想要的结果。
假设你有一个数组(希望你知道 that 是什么)你可以做:
int choose = myNumbers[(int)Math.random() * myNumbers.length];
如果您想要三个值中的每一个,但顺序是随机的,那么您可以将它们放在一个数组中。并且,洗牌该数组,您可以使用 Arrays.asList(T...)
and Collections.shuffle(List)
. Next, iterate the values in the array (perhaps with a for-each
loop)。像
public static void main(String[] args) {
Integer[] arr = new Integer[] { 1, 6, 3 };
Collections.shuffle(Arrays.asList(arr));
for (int i : arr) {
System.out.println(i);
}
}
但是,如果您真的需要一次只选择一个随机值(比如 10 次),那么您可以使用 Random
and nextInt(int)
和 for
循环。这可能看起来像
public static void main(String[] args) {
int[] arr = new int[] { 1, 6, 3 };
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int index = rand.nextInt(arr.length);
System.out.println(arr[index]);
}
}
您必须创建一个数组,其中包含您想要随机获取的值。像这样:
int [] values = {1,3,6};
而且您永远不必忘记输入:
Random rand = new Random();
因为如果不放,随机函数将无法正常工作。
那么,现在你可以随机访问你的值了,就像这样:
int numObtained = rand.nextInt((values.length - 0) + 1) + 0;
最终代码为:
int [] values = {1,3,6};
Random rand = new Random();
int numObtained = rand.nextInt((values.length - 0) + 1) + 0;
希望对您有所帮助!
这可能会有所帮助。
int i[] = new int[]{3,4,6, 5, 8, 10, 14}; // given sets of array
int randomIndex = (int) (Math.random() * i.length); // generates a random index
System.out.println("random selected value " + i[randomIndex]);