【问题标题】:How do I change all double values into a random number? Java [duplicate]如何将所有双精度值更改为随机数? Java [重复]
【发布时间】:2021-03-30 22:44:21
【问题描述】:

我想知道如何将所有 50 个双变量变为随机数?

class Main {
 public static void main(String[] args) {
     double[] numList = new double[50];

 }
}

【问题讨论】:

  • 循环 50 次,每次循环创建一个随机双倍。 Math.random 生成一个介于 0 和 1 之间的随机双精度数。
  • new Random().doubles(50).toArray()var random = new Random(); 并重复 numList[i] = random.nextDouble()
  • 如果使用接受的答案,请使用Arrays.setAll(numList, i -> Math.random());(没有理由将元素设置两次)((并且 Math.random 非常旧......))(((和numList = new Random().doubles(50).toArray()不会需要创建一个额外的(合成)方法)))

标签: java arrays


【解决方案1】:

Arrays#setAll

引用 Javadoc:

设置指定数组的所有元素,使用提供的生成器函数计算每个元素。

i -> Math.random() 部分提供我们的生成器功能。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        double[] numList = new double[50];
        Arrays.setAll(numList, i -> Math.random());
        System.out.println(Arrays.toString(numList));
    }
}

样本运行的输出:

[0.435538​​25429447723, 0.808120954335018, 0.12393048842865151, 0.8280050339559442, 0.13046729109938549, 0.46971055415131957, 0.0847544966916538, 0.9514556138831699, 0.0027874719368471412, 0.2826487547173526, 0.9613735880245629, 0.511374218080356, 0.4181880179563505, 0.6794710449125567, 0.11245752761803662, 0.7441683829312368, 0.18231946738188654, 0.033135972701688, 0.6768385881323342, 0.05649510937114155, 0.7119571752891306, 0.4948538317612915, 0.5744063857753579, 0.09744198319274944, 0.5855447565472038, 0.8802179102691605, 0.2335661700783871, 0.1223125934194853, 0.21850974856355443, 0.0729073196898683, 0.6466093693534394, 0.2427728538560776, 0.6909697855419995, 0.5490190380915355, 0.9021970128518985, 0.18252974719305692, 0.8189241745090639, 0.7901262001450492, 0.1732578238075676, 0.7133774337961251, 0.40475245364411927, 0.549448711998736, 0.2489547800535582, 0.03336023605789973, 0.22881799744124198, 0.16306093388668053, 0.09097495280977719, 0.966965444050888, 0.7512014132665311, 0.3568509914370782]

或者,

import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        double[] numList = IntStream.range(0, 50)
                    .mapToDouble(i -> Math.random()).toArray();
        System.out.println(Arrays.toString(numList));
    }
}

或者,

import java.util.Arrays;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        double[] array = new Random().doubles(50).toArray();
        System.out.println(Arrays.toString(array));
    }
}

或者,

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] args) {
        double[] array = ThreadLocalRandom.current().doubles(50).toArray();
        System.out.println(Arrays.toString(array));
    }
}

或者,

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] args) {
        final int SIZE = 50;
        double[] array = new double[SIZE];

        for (int i = 0; i < SIZE; i++) {
            array[i] = ThreadLocalRandom.current().nextDouble();
        }
        
        System.out.println(Arrays.toString(array));
    }
}

或者,

import java.util.Arrays;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Double[] array = Stream.generate(Math::random).limit(50).toArray(Double[]::new);
        System.out.println(Arrays.toString(array));
    }
}

或者,

import java.util.Arrays;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        double[] array = Stream.generate(Math::random).limit(50).mapToDouble(Double::valueOf).toArray();
        System.out.println(Arrays.toString(array));
    }
}

【讨论】:

    【解决方案2】:

    如果任何随机双精度都足够了,那么只需使用for 循环将每个索引设置为等于Math.random(),这将返回一个介于 0 和 1 之间的随机双精度。


    如果您需要更广泛的随机双打,那么这个表达式...

    Math.random() * upperBound + lowerBound
    

    ...将在(lowerBound, upperBound)范围内生成一个随机数

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      相关资源
      最近更新 更多