3

My goal is to ask the user for an array length and generate a list of random doubles to fill that array out. I don't understand how to get Math.random into my array. I started with a different approach and scrapped it. Why does double random = Math.random() * array; not import a random generator into my array?

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

public class Average
{
  public static void main(String[] args)
  {
    /*Scanner in = new Scanner (System.in);

    System.out.println("Please enter your array size: ");
    int size = in.nextInt();
    int[] awnser = new int[size]; */
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a number");

    double[] array = new double[scanner.nextDdouble()];
    double random = Math.random() * array;
    System.out.println(array.length);

    }
  }
Ashton Meade
  • 33
  • 1
  • 3
  • 4
    You're trying to multiply a `Math.random()` by an array? What are you trying to do with this line? Are you trying to populate the array with random numbers? – GBlodgett Jun 18 '18 at 15:33
  • 3
    how can you instantiate a new array of doubles passing it a non-integer size? – Viç Jun 18 '18 at 15:34
  • 2
    ["*I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.*" - Charles Babbage](https://www.brainyquote.com/quotes/charles_babbage_141832) – Turing85 Jun 18 '18 at 15:35
  • you need to use a for to generate the doubles from 0 to the number that the user put , i recomend to read an integer or at least parse the double to integer value and i dont know if the compiler let you instantiate an array with a double number – MaQuiNa1995 Jun 18 '18 at 15:36

6 Answers6

5

With your current solution you'd need to iterate with a counter starting from 0 and ending before the given size, assigning each array element with a nextDouble invocation.

Multiplying an array object by a number is just not supported in Java.

Here's a Java-8 idiom that you may favor instead:

DoubleStream
    .generate(ThreadLocalRandom.current()::nextDouble)
    .limit(yourGivenSize)
    .toArray();

This will give you a double[] of size yourGivenSize, whose elements are generated randomly.

Mena
  • 47,782
  • 11
  • 87
  • 106
2

Java 8+

You can use ThreadLocalRandom with the Random#doubles method.

import java.util.concurrent.ThreadLocalRandom;
//...
double[] randoms = ThreadLocalRandom.current().doubles(10).toArray();
//Generate 10 random doubles

double[] randoms = ThreadLocalRandom.current().doubles(10, 0, 5).toArray();
//Generate 10 random doubles between 0 (inclusive) and 5 (exclusive)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

Why does double random = Math.random() * array; not import a random generator into my array?

You have misunderstood how this class works. Here is a good explanation of what this class does. What I believe you are trying to do is something along the lines of:

    Random rand = new Random();
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a number");
    //create an array with the size entered by the user
    double[] array = new double[scanner.nextInt()];
    //populate the array with doubles
    for(int i =0; i < array.length; i++) {
        array[i] = rand.nextDouble();
    }

nextDouble returns a pseudorandom number between 0 and 1.0, so you'll need to multiply it by your upper bound. (i.e if 100 was your upper limit rand.nextDouble() * 100;)

(Make sure to import the Random class)

Frontear
  • 1,150
  • 12
  • 25
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
1

Arrays#setAll

import java.util.Arrays;

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

Output of a sample run:

[0.1182317810027168, 0.9437573020895418, 0.2690105524813662, 0.6771923722130754, 0.04893586074165357, 0.42483010937765653, 0.16310798731469023, 0.2541941051963008, 0.9838342001474454, 0.8595732419001068]

Alternatively,

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

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

Alternatively,

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

public class Main {
    public static void main(String[] args) {
        double[] array = new Random().doubles(10).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(10).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 = 10;
        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(10).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(10).mapToDouble(Double::valueOf).toArray();
        System.out.println(Arrays.toString(array));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

I am not entirely sure what you are trying to do, what is the range of the values you are trying to get inside your array ? In any case I think this should achieve what you are trying to do, I am using a command line argument here to supply the array length, feel free to change that back to a scanner.

int arrayLength = Integer.parseInt(args[0]);

double[] array = new double[arrayLength];
for (int i = 0; i < arrayLength; i++)
    array[i] = Math.random() * arrayLength;
System.out.println(Arrays.toString(array));

To generate random values of a specific range:

Min + (int)(Math.random() * ((Max - Min) + 1))
0

I used your original idea to calculate the amount of times you loop to generate your random double numbers. As you know array index (or positions) starts at zero. So I used a for loop to begin at zero and once the number is generated this line: list[i] = num; places the random number into the first index(position) of the array and continues until it reaches the size the user specified.

package stack;

    import java.util.Scanner;
    import java.util.Random;

    public class Stack {

    public static void main(String[] args){

      Scanner input = new Scanner(System.in);
      System.out.println("enter a number");

    int size = input.nextInt();
       Double[] list = new Double[size];

       for(int i = 0; i < size; i++){
            Double num = Math.random();
            list[i] = num;
       }
     System.out.println(list.length);
    }
    }

/*  for(int i = 0; i< list.length; i++){
     System.out.println(list[i]);  }
*/

I also included if you want to check the array contents. I created another for loop with the same concept as the first loop by checking each index(position) and display them.

Desiree
  • 3
  • 5