当前位置:网站首页>Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
2022-07-07 09:28:00 【A Bai|】
Create a length of 6 Of int Type of the array , The value of the array element is required to be 1-30 Between , And random assignment . At the same time, the element values are required to be different .
requirement 1:random produce [ 0 , 1) The random double Type data , Multiply by the range to round , The range of the obtained number is [ Left interval , The right range - 1], Finally, the whole + 1 That is to say [ Left interval + 1, The right range ].
requirement 2: The method is to use while Cycle brush random numbers , This question is relatively simple .
I tested my code several times , Not more than 1ms Of , On the whole, it is relatively fast
package p1.text;
import java.util.*;
/**
* @author WitMoy
* @version V1.8
* @date : 2022-07-02 18:16
*/
public class Main {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
int[] ans = new int[6];
for(int i = 0; i < 6; i++){
ans[i] = (int)(Math.random()* 30 + 1);
while(true){
boolean flag = true;
for(int j = 0; j < i; j++){
if(ans[i] == ans[j]){
flag = false;
break;
}
}
if(flag) break;
ans[i] = (int)(Math.random()* 30 + 1);
}
}
System.out.println(Arrays.toString(ans));
long end = System.currentTimeMillis();
System.out.println("\n when : " + (end - begin) + "ms");
}
}
边栏推荐
猜你喜欢

Postman data driven

嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数

Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes

Binary tree high frequency question type

Postman interface test (II. Set global variables \ sets)

12、 Sort

Mysql database lock learning notes

第一讲:包含min函数的栈

【SVN】SVN是什么?怎么使用?

Variable parameter of variable length function
随机推荐
Implementation of corner badge of Youmeng message push
JS inheritance prototype
Storage of data in memory
软件建模与分析
MySql数据库-事务-学习笔记
sqlplus乱码问题,求解答
C language pointer (Part 2)
Schema-validation: wrong column type encountered in column XXX in table XXX
正则匹配以XXX开头的,XXX结束的
Postman interface debugging method
Unity uses mesh to realize real-time point cloud (I)
Postman setting environment variables
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
在EXCEL写VBA连接ORACLE并查询数据库中的内容
Idea development environment installation
Huawei HCIP - datacom - Core 03 jours
Self awakening from a 30-year-old female programmer
Yapi test plug-in -- cross request
JWT certification used in DRF
Regularly modify the system time of the computer