当前位置:网站首页>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");
}
}
边栏推荐
- Pick up the premise idea of programming
- Selenium mouse sliding operation event
- 数据建模中利用3σ剔除异常值进行数据清洗
- Postman setting environment variables
- 战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
- Regular matching starts with XXX and ends with XXX
- Unity shader (learn more about vertex fragment shaders)
- Interface test API case, data and interface separation
- [SVN] what is SVN? How do you use it?
- DRF authentication, permissions, and flow restrictions (only for views in DRF)
猜你喜欢
随机推荐
软件建模与分析
Loxodonframework quick start
Unity shader (to achieve a simple material effect with adjustable color attributes only)
Mysql database lock learning notes
esp8266使用TF卡并读写数据(基于arduino)
Mysql数据库-锁-学习笔记
Data association between two interfaces of postman
Connecting mobile phone with ADB
Upload taro pictures to Base64
Jenkins modifies the system time
Information Security Experiment 1: implementation of DES encryption algorithm
[cloud native] Devops (I): introduction to Devops and use of code tool
Using JWT to realize login function
Netease Cloud Wechat applet
数据库多表关联查询问题
C language pointer (Part 2)
Some pit avoidance guidelines for using Huawei ECS
Pycharm create a new file and add author information
Install pyqt5 and Matplotlib module
SiteMesh getting started example