简体   繁体   English

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整除的数字。

This code returns a random number with step 10. 0 is excluded from this, but if you want to add it, take out the +1 on the Math.random() line. 该代码在第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);

Math.random() returns a double . Math.random()返回double You want an int value, so you should use the Random class. 您需要一个int值,因此应使用Random类。 You should probably do that anyway. 无论如何,您可能应该这样做。

Random rnd = new Random();
int num = (rnd.nextInt(30) + 1) * 10; // 10, 20, 30, ..., 300

Explanation: 说明:

nextInt(30) returns a random number between 0 and 29 (inclusive). nextInt(30)返回0到29(含)之间的随机数。
+ 1 then makes that a number between 1 and 30. + 1然后使数字介于1到30之间。
* 10 then makes that 10, 20, 30, ..., 300 . * 10然后得出10, 20, 30, ..., 300

So, if you just want 100, 200, 300 , use: 因此,如果您只想要100, 200, 300 ,请使用:

int num = (rnd.nextInt(3) + 1) * 100; // 100, 200, 300

The documentation of Math.random() states that a call returns a Math.random()文档指出,调用返回一个

double value with a positive sign, greater than or equal to 0.0 and less than 1.0. 带正号的double值,大于或等于0.0且小于1.0。

This means, the resulting number of your computation is between 0 and 300, but it is not of type int , but of type double . 这意味着,计算的结果数在0到300之间,但它不是int类型,而是double类型。 You should add a call to Math.round or simply cast it to int and add a loop if you want to create multiple values.. 您应该添加对Math.round的调用,或者只是将其强制转换int并添加一个循环(如果要创建多个值)。

If you wanted to return numbers such as 10, 20, 30, 40, 50, 60... the following should do this. 如果要返回数字,例如10、20、30、40、50、60 ...,则应执行以下操作。

int ranNumber=(int)(Math.random()*10+1)*10;
System.out.println(ranNumber);

//sample output: 80

There doesn't seem to be a direct way to do that but i'm sure with some manipulation we can do it. 似乎没有直接的方法可以做到这一点,但是我敢肯定,通过一些操作,我们就能做到。 Below i have created the method randSeries to generate a number based off of a series. 下面,我创建了randSeries方法来生成基于序列的数字。 You send this method two values, a increment, which is how big you want the base of your series number to be. 您向该方法发送两个值,一个增量,即您希望序列号基数的大小。 Then the base, which is your 10s, 20s, 30s. 然后是底数,即您的10s,20s,30s。 We are essentially generating a random number in the range you provide the method, then multiplying it by the base you sent the method to create a value that is divisible by your base. 我们实质上是在您提供的方法范围内生成一个随机数,然后将其乘以您发送该方法的基数,以创建一个可以被基数整除的值。

    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
    }

As a note :: This method can be used to generate numbers of ANY base value not just 10. 注意:::此方法可用于生成任何基数的数字,而不仅仅是10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM