当前位置:网站首页>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");
}
}
边栏推荐
- 答案在哪里?action config/Interceptor/class/servlet
- Schema-validation: wrong column type encountered in column XXX in table XXX
- Jemter operation
- What is MD5
- NETCORE 3.1 solves cross domain problems
- Postman setting environment variables
- Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
- 网易云微信小程序
- 【云原生】DevOps(一):DevOps介绍及Code工具使用
- 战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
猜你喜欢
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
nlohmann json
Locust performance test 2 (interface request)
Mysql database index study notes
Selenium mouse sliding operation event
其实特简单,教你轻松实现酷炫的数据可视化大屏
Locust performance test 4 (custom load Policy)
Loxodonframework quick start
Connecting mobile phone with ADB
JVM garbage collection detailed learning notes (II)
随机推荐
【SVN】SVN是什么?怎么使用?
Mysql database transaction learning notes
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
STM32 and motor development (from stand-alone version to Networking)
信息安全实验一:DES加密算法的实现
进程和线程的区别
Register address name mapping
DRF authentication, permissions, and flow restrictions (only for views in DRF)
华为HCIP-DATACOM-Core_03day
esp8266使用TF卡并读写数据(基于arduino)
CMD startup software passes in parameters with spaces
Summary of PMP learning materials
5A summary: seven stages of PMP learning
Integer or int? How to select data types for entity classes in ORM
Regular matching starts with XXX and ends with XXX
Jenkins modifies the system time
IIS redirection redirection appears eurl axd
Unity shader (pass user data to shader)
Difference between interface iterator and iteratable
四、机器学习基础