Java Math.Random() 用于数字系列
Java Math.Random() for series of numbers
是否可以在 Java 中使用 Math.Random() 来获取一系列数字,例如 10、20、30、40... 或 100、200、300... .
我当前的实现是 Math.Random()*3*100,因为我认为这将使我的数字达到 300,可以被 100 整除。
documentation of Math.random() 声明调用 returns a
double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
这意味着,你计算的结果数在0到300之间,但它不是int
类型,而是double
类型。如果要创建多个值,您应该添加对 Math.round 的调用或简单地将其转换为 int
并添加一个循环..
如果您想 return 数字,例如 10、20、30、40、50、60...,请按以下步骤操作。
int ranNumber=(int)(Math.random()*10+1)*10;
System.out.println(ranNumber);
//sample output: 80
Math.random()
returns a double
. You want an int
value, so you should use the Random
class。无论如何你应该这样做。
Random rnd = new Random();
int num = (rnd.nextInt(30) + 1) * 10; // 10, 20, 30, ..., 300
解释:
nextInt(30)
returns 0 到 29(含)之间的随机数。
+ 1
然后将其设为 1 到 30 之间的数字。
* 10
然后使 10, 20, 30, ..., 300
.
因此,如果您只想 100, 200, 300
,请使用:
int num = (rnd.nextInt(3) + 1) * 100; // 100, 200, 300
似乎没有直接的方法可以做到这一点,但我相信通过一些操作我们可以做到。下面我创建了方法 randSeries
来生成基于系列的数字。您向此方法发送两个值,一个增量,即您希望序列号的基数有多大。然后是基础,即您的 10 多岁、20 多岁、30 多岁。我们实质上是在您提供方法的范围内生成一个随机数,然后将其乘以您发送方法的基数以创建一个可以被您的基数整除的值。
public int randSeries(int increment, int base){
Random rand = new Random();
int range = rand.nextInt(increment)+1; //random number
System.out.println(range);
int finalVal = range * base; //turning your random number into a series
System.out.println(finalVal);
return finalVal; //return
}
注意:: 此方法可用于生成 任何 基值的数字,而不仅仅是 10.
此代码returns一个随机数,步长为10。0被排除在外,但如果要添加它,请在Math.random()行中取出+1。
int step = 10;
int random = (int)(Math.random()*10+1)*step; //10 is the number of possible outcomes
System.out.println("Random with step " + step + ":" random);
是否可以在 Java 中使用 Math.Random() 来获取一系列数字,例如 10、20、30、40... 或 100、200、300... . 我当前的实现是 Math.Random()*3*100,因为我认为这将使我的数字达到 300,可以被 100 整除。
documentation of Math.random() 声明调用 returns a
double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
这意味着,你计算的结果数在0到300之间,但它不是int
类型,而是double
类型。如果要创建多个值,您应该添加对 Math.round 的调用或简单地将其转换为 int
并添加一个循环..
如果您想 return 数字,例如 10、20、30、40、50、60...,请按以下步骤操作。
int ranNumber=(int)(Math.random()*10+1)*10;
System.out.println(ranNumber);
//sample output: 80
Math.random()
returns a double
. You want an int
value, so you should use the Random
class。无论如何你应该这样做。
Random rnd = new Random();
int num = (rnd.nextInt(30) + 1) * 10; // 10, 20, 30, ..., 300
解释:
nextInt(30)
returns 0 到 29(含)之间的随机数。
+ 1
然后将其设为 1 到 30 之间的数字。
* 10
然后使 10, 20, 30, ..., 300
.
因此,如果您只想 100, 200, 300
,请使用:
int num = (rnd.nextInt(3) + 1) * 100; // 100, 200, 300
似乎没有直接的方法可以做到这一点,但我相信通过一些操作我们可以做到。下面我创建了方法 randSeries
来生成基于系列的数字。您向此方法发送两个值,一个增量,即您希望序列号的基数有多大。然后是基础,即您的 10 多岁、20 多岁、30 多岁。我们实质上是在您提供方法的范围内生成一个随机数,然后将其乘以您发送方法的基数以创建一个可以被您的基数整除的值。
public int randSeries(int increment, int base){
Random rand = new Random();
int range = rand.nextInt(increment)+1; //random number
System.out.println(range);
int finalVal = range * base; //turning your random number into a series
System.out.println(finalVal);
return finalVal; //return
}
注意:: 此方法可用于生成 任何 基值的数字,而不仅仅是 10.
此代码returns一个随机数,步长为10。0被排除在外,但如果要添加它,请在Math.random()行中取出+1。
int step = 10;
int random = (int)(Math.random()*10+1)*step; //10 is the number of possible outcomes
System.out.println("Random with step " + step + ":" random);