如何使用Java在一定范围内生成随机数? [重复]

时间:2022-11-25 20:11:03

This question already has an answer here:

这个问题在这里已有答案:

I want to generate a random number in Java. It can be of integer, byte or float type, but all I really need it is to generate a random number. This is what I'm doing:

我想用Java生成一个随机数。它可以是整数,字节或浮点类型,但我真正需要的是生成一个随机数。这就是我正在做的事情:

  1. Generate a random number within a certain range (e.g. 5 through 20).
  2. 生成一定范围内的随机数(例如5到20)。

  3. Take the number and store it within a variable.
  4. 取数字并将其存储在变量中。

  5. Perform arithmetic on it.
  6. 对它执行算术运算。

Here's the code:

这是代码:

import java.util.HashMap;

public class Attack {
    public static void main(String[] args) {
        HashMap<String, Integer> attacks = new HashMap<String, Integer>();
        attacks.put("Punch", 1);
        attacks.put("Uppercut", 3);
        attacks.put("Roundhouse Kick", 5);

        int actionPoints = // Code for random number generation

        System.out.println("A brigade integrant appeared!");
        System.out.println("What do you do?");
        System.out.println("1: Punch [1 AP], 2: Uppercut [3 AP], 3: Roundhouse Kick [5 AP]");
        System.out.println("You have " + actionPoints + " Action Points.");
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = reader.nextInt();
        reader.close();

        if n == 1 {
            System.out.println("The brigade integrant takes 2 HP of damage!");
        }
        else if n == 2 {
            System.out.println("The brigade integrant takes 5 HP of damage!");
        }
        else if n == 3 {
            System.out.println("The brigade integrant takes 8 HP of damage!");
        }
    }
}

3 个解决方案

#1


4  

In Java 1.7+ you can do it in one line (not counting the import statement ;):

在Java 1.7+中,您可以在一行中完成(不包括import语句;):

import java.util.concurrent.ThreadLocalRandom;

int actionPoints = ThreadLocalRandom.current().nextInt(5, 21); // 5 to 20 inclusive

#2


1  

Try this :

试试这个 :

int lower = 12;
int higher = 29;

int random = (int)(Math.random() * (higher-lower)) + lower;

#3


0  

There are multiple options for you to generate a random number. Two of these would be:

您可以通过多种方式生成随机数。其中两个是:

Math.random(); // Random values ranging from 0 to 1

Random rand; rand.nextInt(x); // Random int ranging from 0 to x

To specify the exact range you could do something like this:

要指定确切的范围,您可以执行以下操作:

int RandomNumber = Min + (int)(Math.random() * Max); 

#1


4  

In Java 1.7+ you can do it in one line (not counting the import statement ;):

在Java 1.7+中,您可以在一行中完成(不包括import语句;):

import java.util.concurrent.ThreadLocalRandom;

int actionPoints = ThreadLocalRandom.current().nextInt(5, 21); // 5 to 20 inclusive

#2


1  

Try this :

试试这个 :

int lower = 12;
int higher = 29;

int random = (int)(Math.random() * (higher-lower)) + lower;

#3


0  

There are multiple options for you to generate a random number. Two of these would be:

您可以通过多种方式生成随机数。其中两个是:

Math.random(); // Random values ranging from 0 to 1

Random rand; rand.nextInt(x); // Random int ranging from 0 to x

To specify the exact range you could do something like this:

要指定确切的范围,您可以执行以下操作:

int RandomNumber = Min + (int)(Math.random() * Max);