用随机数填充数组
我需要使用构造函数创建一个数组,添加一个方法来将数组作为一个序列打印,一个方法用double类型的随机数填充数组。
这是我到目前为止所做的:
import java.util.Random; public class NumberList { private static double[] anArray; public static double[] list(){ anArray = new double[10]; return anArray; } public static void print(){ for(double n: anArray){ System.out.println(n+" "); } } public static double randomFill(){ Random rand = new Random(); int randomNum = rand.nextInt(); return randomNum; } public static void main(String args[]) { } }
我正在努力弄清楚如何使用randomFill方法中生成的随机数填充数组。 谢谢!
您需要添加逻辑以使用randomFill方法将随机值分配给double []数组。
更改
public static double[] list(){ anArray = new double[10]; return anArray; }
至
public static double[] list() { anArray = new double[10]; for(int i=0;i
然后你可以在main方法中调用方法,包括list()和print()来生成随机double值并在console中打印double []数组。
public static void main(String args[]) { list(); print(); }
一个结果如下:
-2.89783865E8 1.605018025E9 -1.55668528E9 -1.589135498E9 -6.33159518E8 -1.038278095E9 -4.2632203E8 1.310182951E9 1.350639892E9 6.7543543E7
这看起来有点像家庭作业。 所以我会给你一些提示。 好消息是你快到了! 你已经完成了大部分的艰苦工作!
- 考虑一个可以帮助您迭代数组的构造。 是否有某种构造(可能是一个循环 ?),您可以使用它来迭代数组中的每个位置?
- 在此构造中,对于循环的每次迭代,您将
randomFill()
返回的值分配给数组的当前位置。
注意:您的数组是double
,但是您要从randomFill
返回int
。 所以你需要解决一些问题。
您可以循环遍历数组,并在每次迭代中调用randomFill方法。 一探究竟:
import java.util.Random; public class NumberList { private static double[] anArray; public static double[] list(){ anArray = new double[10]; return anArray; } public static void print(){ for(double n: anArray){ System.out.println(n+" "); } } public static double randomFill(){ Random rand = new Random(); int randomNum = rand.nextInt(); return randomNum; } public static void main(String args[]) { list(); for(int i = 0; i < anArray.length; i++){ anArray[i] = randomFill(); } print(); } }
您可以在Random类中使用IntStream(ints)或DoubleStream double()从java 8开始。 像这样的东西会起作用,取决于你是想要双重还是整数等。
int [] array = new int [100000];
随机随机= new Random();
array = random.ints(100000,10,100000).toArray();
你可以打印数组,你将获得100000随机整数。
尝试这个:
public void double randomFill(){ Random rand = new Random(); for(int i = 0; i < this.anArray.length(); i++){ this.anArray[i] = rand.nextInt(); } }
你可以在循环中调用randomFill()
方法,并像这样在main()
方法中填充你的数组。
public static void main(String args[]) { for(int i=0;i
虽然你需要在rand.nextDouble()
中使用rand.nextDouble()
,因为你似乎有一个double
数组来填充。
double randomNum = rand.nextDouble(); return randomNum;
您可以使用for-loop
简单地解决它
private static double[] anArray; public static void main(String args[]) { anArray = new double[10]; // create the Array with 10 slots Random rand = new Random(); // create a local variable for Random for (int i = 0; i < 10; i++) // create a loop that executes 10 times { anArray[i] = rand.nextInt(); // each execution through the loop // generates a new random number and // stores it in the array at the // position i of the for-loop } printArray(); // print the result } private static void printArray() { for (int i = 0; i < 10; i++) { System.out.println(anArray[i]); } }
下次,请使用此网站的搜索,因为这是这个网站上一个非常常见的问题,你也可以在谷歌上找到很多解决方案。
如果像这样控制随机性,则总是可以生成给定范围的任意数量的数据点。 如果仅使用java中的随机方法,则不能保证所有数字都是唯一的。
package edu.iu.common; import java.util.ArrayList; import java.util.Random; public class RandomCentroidLocator { public static void main(String [] args){ int min =0; int max = 10; int numCentroids = 10; ArrayList values = randomCentroids(min, max, numCentroids); for(Integer i : values){ System.out.print(i+" "); } } private static boolean unique(ArrayList arr, int num, int numCentroids) { boolean status = true; int count=0; for(Integer i : arr){ if(i==num){ count++; } } if(count==1){ status = true; }else if(count>1){ status =false; } return status; } // generate random centroid Ids -> these Ids can be used to retrieve data // from the data Points generated // simply we are picking up random items from the data points // in this case all the random numbers are unique // it provides the necessary number of unique and random centroids private static ArrayList randomCentroids(int min, int max, int numCentroids) { Random random = new Random(); ArrayList values = new ArrayList (); int num = -1; int count = 0; do { num = random.nextInt(max - min + 1) + min; values.add(num); int index =values.size()-1; if(unique(values, num, numCentroids)){ count++; }else{ values.remove(index); } } while (!( count == numCentroids)); return values; } }
这将为您提供一个包含50个随机数的数组,并显示数组中的最小数字。 我在我的编程课中完成了一项任务。
public static void main(String args[]) { // TODO Auto-generated method stub int i; int[] array = new int[50]; for(i = 0; i < array.length; i++) { array[i] = (int)(Math.random() * 100); System.out.print(array[i] + " "); int smallest = array[0]; for (i=1; i
}`
可能是Java 8中最干净的方法:
private int[] randomIntArray() { Random rand = new Random(); return IntStream.range(0, 23).map(i -> rand.nextInt()).toArray(); }
人们不会在Java库中看到很酷的Stream生产者。
public static double[] list(){ return new Random().ints().asDoubleStream().toArray(); }