-2

I was wondering how do I change all 50 double variables into random numbers?

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

 }
}
  • 1
    Loop 50 times, creating a random double each pass through the loop. Math.random generates a random double between 0 and 1. – Gilbert Le Blanc Mar 30 '21 at 22:50
  • `new Random().doubles(50).toArray()` or `var random = new Random();` and repeat `numList[i] = random.nextDouble()` –  Mar 30 '21 at 22:51
  • please use `Arrays.setAll(numList, i -> Math.random());` if using accepted answer (no reason to set the elements twice) ((and Math.random is very old....)) ((( and `numList = new Random().doubles(50).toArray()` would not need an additional (synthetic) method to be created))) –  Mar 30 '21 at 23:03

2 Answers2

2

Arrays#setAll

To quote the Javadoc:

Set all elements of the specified array, using the provided generator function to compute each element.

The i -> Math.random() part is providing our generator function.

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

Output of a sample run:

[0.43553825429447723, 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]

Alternatively,

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

Alternatively,

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

Alternatively,

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

Alternatively,

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

Alternatively,

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

Alternatively,

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));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

If any random double will suffice then just use a for loop to set each index equal to Math.random() which will return a random double between 0 and 1.


If you need a wider range of random doubles then this expression...

Math.random() * upperBound + lowerBound

...will generate a random number on the range (lowerBound, upperBound)

thehale
  • 913
  • 10
  • 18