java-把生成的随机数,指定范围(如:100-200),指定打印次数(如:50次),并进行去重。
2024-10-09 07:05:21
package main.demo; public class Demo4 {
/**
* 随机指定范围内N个不重复的数
* 最简单最基本的方法
* @param min 指定范围最小值
* @param max 指定范围最大值
* @param
*/
public static int[] randomCommon(int min, int max, int n){
if (n > (max - min + 1) || max < min) {
return null;
}
int[] result = new int[n];
int count = 0;
while(count < n) {
int num = (int) (Math.random() * (max - min)) + min;
boolean flag = true;
for (int j = 0; j < n; j++) {
if(num == result[j]){
flag = false;
break;
}
}
if(flag){
result[count] = num;
count++;
}
}
return result;
}
public static void main(String[] args){
//生成100到200之间的随机数,并打印50次不重样
int[] re = randomCommon(100,200,50);
for (int i : re) {
System.out.println(i);
}
}
}
最新文章
- winform中的确定取消
- 【原创】.NET Web API之filter ActionFilterAttribute 过滤器使用
- sqlserver 简单的事物用法
- 《zw版&#183;Halcon-delphi系列原创教程》 2d照片-3d逆向建模脚本
- 学习总结 java 异常
- 安装qc 出现error An error occurred while attempting to connect to the database.
- 追踪CPU跑满 堆栈调试
- equals和hashcode
- Smarty 插件开发
- 一位6年老Android面经总结
- WebView内容自适应
- Apollo 9 — adminService 主/灰度版本发布
- QT开发环境搭建
- golang学习笔记 ----读写文件
- Java动态代理(一)动态类Proxy的使用
- MYSQL复习笔记2-自带工具介绍
- Android中的Manifest.permission(应用权限)整理
- Ubuntu(Debian)apt-get
- Android - 错误:Unable to instantiate application
- 黑马MySQL数据库学习day01 MySQL8和MySQL5.5暴力破解密码