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

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. css清除默认样式

    CSS 清除默认样式   通常有以下几句就够了: *{margin:0;padding:0} li{list-style:none} img{vertical-align:top;border:non ...

  2. 纯css实现元素下出现横线动画(background-image)

    效果图: html: <div class='site_bar'>首页</div> css: .site_bar{ background-image : linear-grad ...

  3. spring ApplicationContext中Bean的生命周期

    AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...

  4. 13、多进程multiprocessing、进程池

    内容相关: multiprocessing: 进程的创建与运行 进程常用相关函数 进程池: 为什么要有进程池 进程池的创建与运行:串行.并行 回调函数 多进程multiprocessing: pyth ...

  5. OneAlert 携手 BearyChat(倍洽)快速构建 IT 运维 on-call 机制

    OneAlert 是北京蓝海讯通科技股份有限公司旗下产品,中国第⼀个 SaaS 模式的免费的云告警平台,集成国内外主流监控/⽀撑系统,实现⼀个平台上集中处理所有 IT 事件,提升 IT 可靠性.并且能 ...

  6. 使用wxpy自动发送微信消息(加强版)

    通过使用wxpy自动发送微信消息后,笔者又加强了发送消息,堪称消息爆炸式发送 目前设置的为10秒发送一次,发送9次,每次发送10条内容 import requests import wxpy from ...

  7. 【PAT】B1075 链表元素分类(25 分)

    这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...

  8. 【PAT】1083 是否存在相等的差(20 分)

    //这题不是我耍流氓,实在太简单,只能直接贴代码了,凑个数 #include<stdio.h> int aaa[10005]={0}; int main(){ int N;scanf(&q ...

  9. shell脚本之数组

    变量:存储单个元素的内存空间. 数组:存储多个元素的连续的内存空间. 数组名:整个数组只有一个名字: 数组索引:编号从0开始: 数组名[索引]: 引用数组中的某个元素:${ ARRAY_NAME [ ...

  10. Department and Student

    软工结对作业之二 本人ID:杨光海天 031502634 队友(大佬)ID:陈涵 031502106 GitHub链接 BIN文件地址 代码文件 整体概况 模型建立 学生类,属性包括: * 1)编号 ...