-4
import java.util.Scanner;
import java.util.Random;

public class Lab04b 
{       
    public static void main(String []args)
    {    
        Random generator = new Random (); 
        Scanner scan = new Scanner(System.in);

        int num1;
        int num2;
        int num3;

        System.out.println("Enter X:");
        num1 = scan.nextInt();

        System.out.println("Enter Y:");
        num2 = scan.nextInt();

        num3 = generator.nextInt(num2) + num1;
        System.out.println("3 random integers in the range " + num1 + ".." + num2 + " are: " + num3);
    }
}

我被困在如何在 x 和 y 范围之间获得 3 个随机整数。Y 为最大整数。

4

5 回答 5

1

x诀窍是找到和之间的区别y。这是你需要做的 -

int diff = Math.abs(num1 - num2);
num3 = generator.nextInt(diff) + Math.min(num1, num2);

只需执行 3 次即可获得 3 个数字。

于 2013-09-25T06:42:03.850 回答
1

从文档

nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0
(inclusive) and the specified value (exclusive), drawn from this
random number generator's sequence.

所以 random.nextInt(Y) 会给你数字 0..Y,我猜你错过了如何正确获得下限。

X + random.nextInt(YX) 可以解决问题。

于 2013-09-25T06:44:30.677 回答
0

首先有一个循环运行 3 次,生成 3 个随机数(正如你所说,你需要 3 个随机数,但你只生成 1 个)。

接下来,您用来生成随机数的模式似乎存在缺陷。您可以使用以下类型 1 模式来完成此操作。

min + Math.random() * ((max - min) + 1));

或者这种类型 2 模式

rand.nextInt((max - min) + 1) + min;

所以你可以做这样的事情: -

for (int i = 0; i < 3; i++) {
    // Type 1
    num3 = num1 + (int)(Math.random() * ((num2 - num1) + 1));
    // Type 2
    // num3 = generator.nextInt((num2 - num1) + 1) + num1;
    System.out.println("3 random integers in the range " + num1 + ".." + num2 + " are: " + num3);
}

PS:-您需要自己确定最大值和最小值。我刚刚给出了模式和样本。

于 2013-09-25T06:41:28.887 回答
0

阅读文档。该nextInt(n)函数返回一个Integerin [0, n)。所以,在你的情况下,你可以使用公式min + nextInt(max-min),它会给你一个数字[min, max)

Random generator = new Random(); 
int max = (x >= y ? x : y);
int min = (x < y ? x : y);
int aRandomNumber = min + generator.nextInt(max-min);
于 2013-09-25T06:42:46.790 回答
0
import java.util.Scanner;

public class GenerateRandomX_Y_numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the numbers x and y: ");
        int x = Math.abs(sc.nextInt()), y = Math.abs(sc.nextInt());//we need //non-negative integers, that is why we use here Math.abs. which means the //absolute value

        print3RandomNumbers_between_x_and_y(x, y);

    }

public static void print3RandomNumbers_between_x_and_y(int x, int y) {//here //I create a method with void type that takes two int inputs
    boolean isTrue = (x < y);//according to our conditions X should less //than Y

    if (isTrue) {//if the condition is true do => generate three int in the //range x .... y
        int rand1 = (int) (Math.random() * (y - x) + 1);// y - x means our //range, we then multiply this substraction by Math.random()
        int rand2 = (int) (Math.random() * (y - x) + 1);//the productof this //multiplication we cast to int type that is why we have 
        int rand3 = (int) (Math.random() * (y - x) + 1);//(int) before //(Math.random() * (y - x));

        System.out.println("rand1 = " + rand1);//
        System.out.println("rand2 = " + rand2);//
        System.out.println("rand3 = " + rand3);//here print our result

    } else
        System.out.println("Error input: X should be less than Y. Try it again!");//if the condition is not true, i mean if x is not less than or equal //to Y, print this message
}

}

于 2015-06-26T10:43:40.493 回答