当前位置:网站首页>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");
}
}
边栏推荐
- Cesium load vector data
- 软件建模与分析
- 网易云微信小程序
- NETCORE 3.1 solves cross domain problems
- Information Security Experiment 4: implementation of IP packet monitoring program
- shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
- LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
- Binary tree high frequency question type
- Kubernetes cluster capacity expansion to add node nodes
- stm32和电机开发(从单机版到网络化)
猜你喜欢
Mysql database transaction learning notes
信息安全实验二 :使用X-SCANNER扫描工具
Jmeters use
JVM garbage collection detailed learning notes (II)
Summary of PMP learning materials
Unity shader (to achieve a simple material effect with adjustable color attributes only)
Mysql database lock learning notes
Pycharm create a new file and add author information
Jenkins task grouping
数据建模中利用3σ剔除异常值进行数据清洗
随机推荐
STM32 clock system
Windows starts redis service
进程间的通信方式
How to speed up video playback in browser
Detailed learning notes of JVM memory structure (I)
Jenkins automated email
Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
Interface test API case, data and interface separation
信息安全实验三 :PGP邮件加密软件的使用
Netease cloud wechat applet
Unity shader (data type in cghlsl)
牛客网——华为题库(61~70)
網易雲微信小程序
Cesium load vector data
4、 Fundamentals of machine learning
Test Engineer Interview Questions 2022
Variable parameter of variable length function
Record of structured interview
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
DRF defines views and routes