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);
}
}
}

最新文章

  1. winform中的确定取消
  2. 【原创】.NET Web API之filter ActionFilterAttribute 过滤器使用
  3. sqlserver 简单的事物用法
  4. 《zw版&#183;Halcon-delphi系列原创教程》 2d照片-3d逆向建模脚本
  5. 学习总结 java 异常
  6. 安装qc 出现error An error occurred while attempting to connect to the database.
  7. 追踪CPU跑满 堆栈调试
  8. equals和hashcode
  9. Smarty 插件开发
  10. 一位6年老Android面经总结
  11. WebView内容自适应
  12. Apollo 9 — adminService 主/灰度版本发布
  13. QT开发环境搭建
  14. golang学习笔记 ----读写文件
  15. Java动态代理(一)动态类Proxy的使用
  16. MYSQL复习笔记2-自带工具介绍
  17. Android中的Manifest.permission(应用权限)整理
  18. Ubuntu(Debian)apt-get
  19. Android - 错误:Unable to instantiate application
  20. 黑马MySQL数据库学习day01 MySQL8和MySQL5.5暴力破解密码

热门文章

  1. PHP利用FTP上传文件连接超时之开启被动模式解决方法
  2. Axis WSDD文件参考文档
  3. 使用 Masstransit中的 Request/Response 与 Courier 功能实现最终一致性
  4. el-table表头与内容右边框错位问题
  5. cf # 420 div.2
  6. linux下将多个ts文件合并为一个MP4文件
  7. 手把手教你把web应用丢到服务器上(单页应用+ 服务端渲染)
  8. Eclipse中Debug时鼠标悬停不能查看变量值解决办法
  9. 【部分】Asp.Net Mvc 控制器与视图的数据传递
  10. (私人收藏)蓝色夜空背景的通用商务PPT模板