编写一个程序,读取两个非负整数'x'和'y'(其中x <y),然后在x ... y范围内打印三个随机整数

时间:2013-09-25 06:35:53

标签: java random integer

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是最大的整数。

5 个答案:

答案 0 :(得分:1)

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

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

只需做3次就可以获得3个号码。

答案 1 :(得分: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(Y-X)可以解决问题。

答案 2 :(得分:0)

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

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

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

type 2 pattern

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);
}

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

答案 3 :(得分:0)

阅读documentationnextInt(n)函数在Integer中返回[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);

答案 4 :(得分: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
}

}