当前位置:网站首页>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");
}
}
边栏推荐
猜你喜欢

網易雲微信小程序

Register address name mapping

信息安全实验四:Ip包监视程序实现

The configuration and options of save actions are explained in detail, and you won't be confused after reading it

正则匹配以XXX开头的,XXX结束的

章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕

信息安全实验二 :使用X-SCANNER扫描工具

Selenium mouse sliding operation event

Vs2013 generate solutions super slow solutions

Detailed learning notes of JVM memory structure (I)
随机推荐
信息安全实验一:DES加密算法的实现
软件建模与分析
Register address name mapping
如何使用clipboard.js库实现复制剪切功能
Yapi test plug-in -- cross request
在EXCEL写VBA连接ORACLE并查询数据库中的内容
How to speed up video playback in browser
Huawei hcip datacom core_ 03day
Data association between two interfaces of postman
12、 Sort
Cesium load vector data
JS judge whether checkbox is selected in the project
[SVN] what is SVN? How do you use it?
【云原生】DevOps(一):DevOps介绍及Code工具使用
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
Regular matching starts with XXX and ends with XXX
Self awakening from a 30-year-old female programmer
Netease cloud wechat applet
Postman interface debugging method
Jenkins+ant+jmeter use