java里面如何获取一个随机数,随机数里面要有正的和负的。

2025-03-22 22:33:23
推荐回答(4个)
回答1:

// 获得一个给定范围的随机整数
public static int getRandomNum(int smallistNum, int BiggestNum) {
Random random = new Random();
return (Math.abs(random.nextInt()) % (BiggestNum - smallistNum + 1))+ smallistNum;
}

回答2:

import java.util.*;
public class Yugi{
    public static void main(String[] args){
        int[] arr = new int[10];
        arr[0] = (int)(Math.random() * (-11));
        for(int i = 1; i < arr.length - 1; i++){
            int rand = (int)(Math.random() * 21) - 10;
        }
        arr[arr.length  - 1] = (int)(Math.random() * 11);
        System.out.println(Arrays.toString(arr));
    }
}

回答3:

https://jingyan.baidu.com/article/5225f26b46bd1ce6fa0908b3.html

回答4:

用scrand试试