
正文
java数组随机数代码 java编程实现对数组随机排序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在java中随机生成10个整数,用户求输入一个数,判断是否存在于这10个整数中?
由于你没有指定这10个整数的生成范围,所以我这里假定是0~99之间的整数,这样用户输入时有10%的几率命中。
整体代码为:
public class Main {
public static void main(String[] args) {
//声明长度为10的随机数数组
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0; i 10; i++) {
//获取0~99之间的一个随机整数,可通过调整nextInt的参数来修改随机数范围
int num = random.nextInt(100);
//如果新生成的数字已经存在于随机数数组中,则重新生成
if (checkDistinct(randoms, num)) {
i--;
continue;
}
randoms[i] = num;
}
//增序排序,好看
Arrays.sort(randoms);
System.out.println("请输入一个整数:");
Scanner scanner = new Scanner(System.in);
//严谨一点这里其实可以对输入的in进行校验,检验其是不是整数,校验方法很多搜一下就有我这就不校验了
int in = scanner.nextInt();
System.out.println("生成的随机数数组为:");
System.out.println(Arrays.toString(randoms));
if (checkDistinct(randoms, in)) {
System.out.println("输入的数字[" + in + "]在其中");
} else {
System.out.println("输入的数字[" + in + "]不在其中");
}
}
//检查新生成的数字是否存在于随机数数组中,若存在,返回true
private static boolean checkDistinct(int[] randoms, int num) {
for (int i = 0; i randoms.length; i++) {
if (randoms[i] == num) {
return true;
}
}
return false;
}
}
运行结果:
输入的数字存在时:
输入的数字不存在时:
相关问答
Q1: java编程实现随机数组的快速排序
java编程实现随机数组的快速排序步骤如下:
1、打开Eclipse,新建一个Java工程,在此工程里新建一个Java类;
2、在新建的类中声明一个产生随机数的Random变量,再声明一个10个长度的int型数组;
3、将产生的随机数逐个放入到数组中;
4、利用排序算法对随机数组进行排序。
具体代码如下:
import java.util.Random;
public class Demo {
public static void main(String[] args) {
int count = 0;
Random random = new Random();
int a[] = new int[10];
while(count 10){
a[count] = random.nextInt(1000);//产生0-999的随机数
count++;
}
for (int i = 0; i a.length - 1; i++) {
int min = i;
for (int j = i + 1; j a.length; j++) {
if (a[j] a[min]) {
min = j;
}
}
if (min != i) {
int b = a[min];
a[min] = a[i];
a[i] = b;
}
}
for (int c = 0; c a.length; c++) {
System.out.print(a[c] + " ");
}
}
}
Q2: 求解 java中怎么给数组赋值随机数
这还不简单啊!如下代码:
int[] ar = new int[20];//初始化数组 长度为20
for (int i = 0; i ar.length; i++) {//循环给数组中的每个元素赋初值
ar[i] = (int) (Math.random() * 100);//产生随机数并赋值给数组
}
for (int i = 0; i len; i++) {//循环打印出来
System.out.println(ar[i]);
}
Q3: java中如何将随机数放到数组里?
首先:javajava数组随机数代码的Math类提供java数组随机数代码了一个
random()静态方法java数组随机数代码,返回带正号的
double
值,该值大于等于
0.0
且小于 1.0。返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。
以下为范例:
//代码如下:
public class Test24 {
/*
* 随机从 1~10 取十个整数,存入数组
*/
public static void main(String[] args) {
int [] arr = new int[10]; //构建一个空的一维数组
for(int i=0;iarr.length;i++){
int temp = (int)(Math.random()*10)+1;//随机产生一个 1~10 的整数
arr[i] = temp;//将产生的数添加到数组
System.out.print(arr[i]+" ");
}
}
}
Q4: 求JAVA生成随机数的代码
import java.util.Random;
public class RandomNumber{
public static void main(String[] args) {
// 使用java.lang.Math的random方法生成随机数
System.out.println("Math.random(): " + Math.random());
// 使用不带参数的构造方法构造java.util.Random对象
System.out.println("使用不带参数的构造方法构造的Random对象:");
Random rd1 = new Random();
// 产生各种类型的随机数
// 按均匀分布产生整数
System.out.println("int: " + rd1.nextInt());
// 按均匀分布产生长整数
System.out.println("long: " + rd1.nextLong());
// 按均匀分布产生大于等于0,小于1的float数[0, 1)
System.out.println("float: " + rd1.nextFloat());
// 按均匀分布产生[0, 1)范围的double数
System.out.println("double: " + rd1.nextDouble());
// 按正态分布产生随机数
System.out.println("Gaussian: " + rd1.nextGaussian());
// 生成一系列随机数
System.out.print("随机整数序列:");
for (int i = 0; i 5; i++) {
System.out.print(rd1.nextInt() + " ");
}
System.out.println();
// 指定随机数产生的范围
System.out.print("[0,10)范围内随机整数序列: ");
for (int i = 0; i 10; i++) {
// Random的nextInt(int n)方法返回一个[0, n)范围内的随机数
System.out.print(rd1.nextInt(10) + " ");
}
System.out.println();
System.out.print("[5,23)范围内随机整数序列: ");
for (int i = 0; i 10; i++) {
// 因为nextInt(int n)方法的范围是从0开始的,
// 所以需要把区间[5,28)转换成5 + [0, 23)。
System.out.print(5 + rd1.nextInt(23) + " ");
}
System.out.println();
System.out.print("利用nextFloat()生成[0,99)范围内的随机整数序列: ");
for (int i = 0; i 10; i++) {
System.out.print((int) (rd1.nextFloat() * 100) + " ");
}
System.out.println();
System.out.println();
// 使用带参数的构造方法构造Random对象
// 构造函数的参数是long类型,是生成随机数的种子。
System.out.println("使用带参数的构造方法构造的Random对象:");
Random ran2 = new Random(10);
// 对于种子相同的Random对象,生成的随机数序列是一样的。
System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i 10; i++) {
System.out.print(ran2.nextInt(10) + " ");
}
System.out.println();
Random ran3 = new Random(10);
System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: ");
for (int i = 0; i 10; i++) {
System.out.print(ran3.nextInt(10) + " ");
}
System.out.println();
// ran2和ran3生成的随机数序列是一样的,如果使用两个没带参数构造函数生成的Random对象,
// 则不会出现这种情况,这是因为在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数。
// 另外,直接使用Random无法避免生成重复的数字,如果需要生成不重复的随机数序列,需要借助数组和集合类
}
}运行结果:
C:\java RandomNumber
Math.random(): 0.525171492959965
使用不带参数的构造方法构造的Random对象:
int: 636539740
long: -752663949229005813
float: 0.87349784
double: 0.4065973309853902
Gaussian: 0.4505871918488808
随机整数序列:1936784917 1339857386 -1185229615 1883411721 1409219372
[0,10)范围内随机整数序列: 1 1 5 5 9 0 1 0 2 4
[5,23)范围内随机整数序列: 9 13 26 18 11 27 26 12 21 8
利用nextFloat()生成[0,99)范围内的随机整数序列: 1 47 72 59 49 86 80 88 55 82
使用带参数的构造方法构造的Random对象:
使用种子为10的Random对象生成[0,10)内随机整数序列:
3 0 3 0 6 6 7 8 1 4
使用另一个种子为10的Random对象生成[0,10)内随机整数序列:
3 0 3 0 6 6 7 8 1 4
java数组随机数代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编程实现对数组随机排序、java数组随机数代码的信息别忘了在本站进行查找喔。







