此为工具类,支持抽奖业务需求,具体实现见下方代码:

package com.org.test;

import java.util.ArrayList;
import java.util.List; public class RandomUtil { /**
* 随机抽出中奖用户下标
* 当max < count时,输出max+1个, 如 0,2,5 输出3个结果
* 当max = count时,输出 count个, 如 0,2,2 输出2个结果
* 当max > count 时,输出count个,如 0,5,3 输出3个结果
* @param min
* @param max
* @param count
* @return
* @author Shindo
* @date 2017年1月7日 下午5:00:14
*/
public static int[] getRandomArray(int min, int max, int count) {
List<Integer> randomCommon = randomCommon(min, max, count);
int size = randomCommon.size();
int[] result = new int[size];
if (size > 0) {
for (int j = 0; j < randomCommon.size(); j++) {
Integer integer = randomCommon.get(j);
result[j] = integer;
}
System.out.println("随机下标:");
for (int k = 0; k < result.length; k++) {
System.out.println(result[k]);
}
}
return result;
} /**
* 随机指定范围内N个不重复的数
* @param min 指定范围最小值
* @param max 指定范围最大值
* @param n 随机数个数
* @return
*/
public static List<Integer> randomCommon(int min, int max, int n) {
List<Integer> integers = new ArrayList<Integer>();
if (min < 0) {
min = 0;
}
if ((max - min) + 1 < n) {
n = (max - min) + 1;
}
if (max < min) {
max = min;
}
if (max < 0 || n < 0) {
return integers;
}
for (int i = 1; i <= n; i++) {
int randomNumber = (int) Math.round(Math.random() * (max - min) + min);
if (integers.contains(randomNumber)) {
i--;
continue;
} else {
integers.add(randomNumber);
}
}
return integers;
} // 测试随机数获取
public static void main(String[] args) { // 当max < count时,输出max+1个,当max = count时,输出 count个, 当max > count 时,输出count个
List<Integer> randomCommon = randomCommon(0, 8, 3); for (int j = 0; j < randomCommon.size(); j++) {
Integer integer = randomCommon.get(j);
System.out.println(integer);
}
System.out.println("\n");
int size = randomCommon.size();
int[] result = new int[size];
if (size > 0) {
for (int j = 0; j < randomCommon.size(); j++) {
Integer integer = randomCommon.get(j);
result[j] = integer;
}
System.out.println("抽奖下标打印输出");
for (int k = 0; k < result.length; k++) {
System.out.println(result[k]);
}
} } }

  

随机指定范围内N个不重复的数的更多相关文章

  1. 随机获取指定范围内N个不重复数字

    /// <summary> /// 随机获取指定范围内N个不重复数字 /// </summary> /// <param name="min"> ...

  2. python获取指定时间段内的随机不重复的时间点

    上篇 <python时间时分秒与秒数的互相转换>http://www.cnblogs.com/gayhub/p/6154707.html 提到了把时间转成秒数的方法, 这篇写写转换成秒数后 ...

  3. C#生成指定范围内的不重复随机数

    C#生成指定范围内的不重复随机数 // Number随机数个数 // minNum随机数下限 // maxNum随机数上限 public int[] GetRandomArray(int Number ...

  4. Oracle 生成指定范围内随机日期

    Oracle生成一个指定范围内的随机日期 /* 年1月1日)的整数偏移量来保存(即把日期保存为一个数字); * 因此可通过寻找‘指定日期’与‘关键日期’相对应的整数偏移量,再加一个指定范围内的随机整数 ...

  5. JS生成指定范围内的随机数(支持随机小数)

    直接需要函数的话,直接到文章的最后面找. ============================================================= 转载:https://www.cn ...

  6. 【VBA】返回指定范围内的随机整数

    返回指定范围内的随机整数: Sub main() Randomize Debug.Print 随机整数(1, 2) End Sub Function 随机整数(a As Integer, b As I ...

  7. Java产生指定范围内的随机日期

    要想产生指定范围内的随机日期,首先我们要指定一个范围,那么我们可以通过SImpleDateFormat格式化日期,然后再通过parse()方法设置日期,返回一个Date类型的日期对象,再转化为时间戳( ...

  8. 关于 Math.random()生成指定范围内的随机数的公式推导

    关于 Math.random()生成指定范围内的随机数的公式推导 在 java 中,用于生成随机数的 Math 方法 random()只能生成 0-1 之间的随机数,而对于生成指定区间,例如 a-b ...

  9. Window Linux下实现指定目录内文件变更的监控方法

    转自:http://qbaok.blog.163.com/blog/static/10129265201112302014782/ 对于监控指定目录内文件变更,window 系统提供了两个未公开API ...

随机推荐

  1. 洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)

    题意 题目链接 Sol 首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血 发现状态数只有\(s = 1 ...

  2. jquery制作移动端菜单栏左右滑动

    //菜单栏滑动function move_scollX(){ var startPosition, endPosition, distanceX,distanceY; $(".left&qu ...

  3. PopupWindow 弹出时背景变暗

    下面的PopupWindow  的高是相对于屏幕高设计,宽是获取的某一个控件的宽设置,位置位于某控件的上方,红色部分是设置弹出时屏幕变暗的. //设置contentView View contentV ...

  4. Git服务器配置及本地克隆提交、服务器获取

    1.服务器Git安装配置 相关链接 相关链接 注意ssh-keygen .修改权限 权限:    相关链接   2.本地获取 git clone name@ip:服务器项目位置 相关链接   3.创建 ...

  5. JsonParseException:非法的unquoted字符((CTRL-CHAR,代码9)):必须被转义

     其它异常,Could not read document: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped  ...

  6. ubuntu通过apt-get安装JDK8

    安装python-software-properties apt-get install python-software-properties apt-get install software-pro ...

  7. [HDFS_1] HDFS 的概念和特性

    0. 参考 HDFS你一定要知道,要考的 大数据开发实战:HDFS和MapReduce优缺点分析 SecondaryNamenode的作用详解 1. HDFS 是什么 HDFS :一种分布式文件系统, ...

  8. 【PAT】B1058 选择题(20 分)

    这道题的逻辑怪复杂的,写起来蛮费时间的 结构体中要储存的信息多,整体不难,信息量大,容易把人搞蒙 #include<stdio.h> #include<string.h> #i ...

  9. 【PAT】B1061 判断题(15 分)

    简单逻辑题, #include<stdio.h> #include<algorithm> using namespace std; int main(){ int N,M;// ...

  10. Appium1.9 之 Chromedriver安装方式

    1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 “No C ...