如何制作一个由 1 和 0 组成的随机数组生成器,长度取自用户
How to make a random array generator that consists of 1's and 0's, with length taken from user
我正在尝试获取一个由 1 和 0 组成的数组,其长度在 运行 之后确定。 "arr1.length" 是事先从用户那里获取的。
private double[] arrayGen(int A) {
double[] arr1 = new double[A];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = (Math.random());
}
这是我到目前为止尝试过的方法,它正在生成 运行dom "double"s,但我无法让它 运行domly 生成 1 和 0。
感谢您的帮助。
约翰
改变
arr1[i] = (Math.random());
至
arr1[i] = (Math.random() < 0.5 ? 0 : 1);
如果您想要整数值的双精度值,生成整数并将它们转换为双精度值可能更简单。
import java.util.Arrays;
import java.util.Random;
public class Test {
public static void main(String[] args) throws Throwable {
System.out.println(Arrays.toString(arrayGen(20)));
}
private static Random rand = new Random();
private static double[] arrayGen(int A) {
double[] arr1 = new double[A];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = rand.nextInt(2);
}
return arr1;
}
}
你也可以替换
arr1[i] = (Math.random());
和
arr1[i] = (int)(Math.random() * 2);
ps:如果将 "double[] arr1 = new double[A];" 更改为 "int[] arr1 = new int[A];"
,则无需转换为 int
您正在调用 Math.random(),它 return 是 0 和 1 之间的随机双精度数。改为:
Random rand = new Random();
double zeroOne = (double) rand.nextInt(2);
rand.nextInt(2);将 return 0 或 1 随机,重要的是要记住 rand.nextInt(value) returns 是一个介于 0 和值之间的随机数,但永远不会 return 值本身, 到这个值只有0.
不确定为什么要使用 double
。
呢
public static void main(String[] args){
Random r = new Random();
int[] values = new int[10];
for(int i=0;i<values.length;i++){
values[i] = r.nextInt(2);
}
System.out.println(Arrays.toString(values));
}
输出
[0, 1, 0, 1, 0, 0, 1, 1, 0, 1]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.lang.reflect.Array;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Ali
*/
public class JavaApplication6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc =new Scanner(System.in);
int[] anArray;
anArray = new int[sc.nextInt()];
for(int i=0;i<anArray.length;i++)
{
anArray[i]= randInt(0,1);
}
for(int i=0;i<anArray.length;i++)
{
System.out.println(anArray[i]);
}
}
public static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
我正在尝试获取一个由 1 和 0 组成的数组,其长度在 运行 之后确定。 "arr1.length" 是事先从用户那里获取的。
private double[] arrayGen(int A) {
double[] arr1 = new double[A];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = (Math.random());
}
这是我到目前为止尝试过的方法,它正在生成 运行dom "double"s,但我无法让它 运行domly 生成 1 和 0。
感谢您的帮助。
约翰
改变
arr1[i] = (Math.random());
至
arr1[i] = (Math.random() < 0.5 ? 0 : 1);
如果您想要整数值的双精度值,生成整数并将它们转换为双精度值可能更简单。
import java.util.Arrays;
import java.util.Random;
public class Test {
public static void main(String[] args) throws Throwable {
System.out.println(Arrays.toString(arrayGen(20)));
}
private static Random rand = new Random();
private static double[] arrayGen(int A) {
double[] arr1 = new double[A];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = rand.nextInt(2);
}
return arr1;
}
}
你也可以替换
arr1[i] = (Math.random());
和
arr1[i] = (int)(Math.random() * 2);
ps:如果将 "double[] arr1 = new double[A];" 更改为 "int[] arr1 = new int[A];"
,则无需转换为 int您正在调用 Math.random(),它 return 是 0 和 1 之间的随机双精度数。改为:
Random rand = new Random();
double zeroOne = (double) rand.nextInt(2);
rand.nextInt(2);将 return 0 或 1 随机,重要的是要记住 rand.nextInt(value) returns 是一个介于 0 和值之间的随机数,但永远不会 return 值本身, 到这个值只有0.
不确定为什么要使用 double
。
public static void main(String[] args){
Random r = new Random();
int[] values = new int[10];
for(int i=0;i<values.length;i++){
values[i] = r.nextInt(2);
}
System.out.println(Arrays.toString(values));
}
输出
[0, 1, 0, 1, 0, 0, 1, 1, 0, 1]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.lang.reflect.Array;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Ali
*/
public class JavaApplication6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc =new Scanner(System.in);
int[] anArray;
anArray = new int[sc.nextInt()];
for(int i=0;i<anArray.length;i++)
{
anArray[i]= randInt(0,1);
}
for(int i=0;i<anArray.length;i++)
{
System.out.println(anArray[i]);
}
}
public static int randInt(int min, int max) {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}