How do I generate random doubles in an Array?

Ashton Meade

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

    }
  }
GBlodgett

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using jfugue, how do I generate random music strings (using player.play) from an array?

How do I generate random numbers in an array that add up to a defined total?

How do I generate a random integer between min and max in Java?

How do I cast an array of doubles into an array of integers in Java?

How do I generate random numbers in Dart?

How do I generate a random int number?

How do I populate an array with random numbers?

How do I generate random points in 3D space?

How do I generate a random alphanumeric array that has 3 letter and 6 digits in c#?

How do I select a random array, and then a random index inside THAT array?

How do I generate a random timestamp in the next 5 days in Python?

How do I generate a random num::BigUint?

How do I generate a random derangement of a list/array in java?

I want to generate a random list of set strings from an array without duplicates, not sure how to do the duplicates part

How do I generate a multidimensional NumPy array with random numbers with the dimension of another array whose dimension is not declared?

How can I generate an array of random numbers that fluctuate by 1 in javascript?

How do I generate random numbers to fill in empty rows?

Javascript, how do i get two corresponding elements from an array to generate at the same time with the random feature?

How do I generate random numbers x times?

How do I generate a primary key that is not random?

How do i generate a random numbers for each integer in an array?

How do I generate random string array urls without repeating?

Python: How do I generate a random matrix of 1.0 and 0.0, but with floats?

In laravel, how do I generate an array of counts?

how do I generate random group names using lists

How do I generate a random integer in C#?

How do I generate a random number between 1 and 0 in SAS?

How do I generate array of random elements that represent the height of bars to plot

How can I generate a Bernoulli random integer array in rust?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive