随机子集的Java实现
来源:百度文库 编辑:神马文学网 时间:2024/04/27 18:22:17
获取一个集合中指定个数的随机子集(元素不重复)的实现。
package com.lavasoft.randomset;
import java.util.*;
/**
* 数学集合概念上的随机子集的Java实现研究代码
*
* @author leizhimin 2010-5-17 13:25:52
*/
public class RundomSubsetToolkit {
public static void main(String[] args) {
// int[] rs = randomSubset(1, 5000, 300);
// for (int r : rs) {
// System.out.println(r);
// }
// List list = new ArrayList(10);
// list.add("a");
// list.add("b");
// list.add("c");
// list.add("d");
// list.add("e");
// list.add("f");
// list.add("g");
// list.add("h");
// list.add("i");
// list.add("j");
// List rs1 = randomSubset(list, 5);
// for (Object o : rs1) {
// System.out.println(o);
// }
//
int[] array = {7, 9, 2, 8, 6, 4, 3};
int[] rs3 = randomSubset(array, 4);
for (int i : rs3) {
System.out.println(i);
}
Map map = new HashMap();
}
/**
* 获取某个范围内指定个数的随机自然数子集
*
* @param beginIndex 取之范围起
* @param endIndex 取之范围止
* @param subCount 子集元素数目
* @return 自然数子集
*/
public static int[] randomSubset(final int beginIndex, final int endIndex, final int subCount) {
if (beginIndex < 0 || endIndex < 1 || subCount < 0 || endIndex - beginIndex < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
int[] rs = new int[subCount];
int rescount = endIndex - beginIndex + 1;
int[] scope = new int[rescount];
for (int i = beginIndex; i <= endIndex; i++)
scope[i - beginIndex] = i;
Random random = new Random();
for (int i = 0, odd = rescount - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = scope[ranindex];
scope[ranindex] = scope[odd];
}
return rs;
}
/**
* 获取一个集合列表指定数目的子集
*
* @param list 集合列表
* @param subCount 子集数目
* @return 子集
*/
public static List randomSubset(final List list, final int subCount) {
if (list == null || list.size() == 0 || list.size() < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
List rs = new ArrayList(subCount);
for (int i = 0; i < subCount; i++) {
rs.add(null);
}
Random random = new Random();
for (int i = 0, odd = list.size() - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs.set(i, list.get(ranindex));
list.set(ranindex, list.get(odd));
}
return rs;
}
/**
* 获取一个long型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static long[] randomSubset(long[] array, int subCount) {
long[] rs = new long[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 获取一个int型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static int[] randomSubset(int[] array, int subCount) {
int[] rs = new int[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 获取一个Object型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static Object[] randomSubset(Object[] array, int subCount) {
Object[] rs = new Object[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
}
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/317297
package com.lavasoft.randomset;
import java.util.*;
/**
* 数学集合概念上的随机子集的Java实现研究代码
*
* @author leizhimin 2010-5-17 13:25:52
*/
public class RundomSubsetToolkit {
public static void main(String[] args) {
// int[] rs = randomSubset(1, 5000, 300);
// for (int r : rs) {
// System.out.println(r);
// }
// List list = new ArrayList(10);
// list.add("a");
// list.add("b");
// list.add("c");
// list.add("d");
// list.add("e");
// list.add("f");
// list.add("g");
// list.add("h");
// list.add("i");
// list.add("j");
// List rs1 = randomSubset(list, 5);
// for (Object o : rs1) {
// System.out.println(o);
// }
//
int[] array = {7, 9, 2, 8, 6, 4, 3};
int[] rs3 = randomSubset(array, 4);
for (int i : rs3) {
System.out.println(i);
}
Map map = new HashMap();
}
/**
* 获取某个范围内指定个数的随机自然数子集
*
* @param beginIndex 取之范围起
* @param endIndex 取之范围止
* @param subCount 子集元素数目
* @return 自然数子集
*/
public static int[] randomSubset(final int beginIndex, final int endIndex, final int subCount) {
if (beginIndex < 0 || endIndex < 1 || subCount < 0 || endIndex - beginIndex < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
int[] rs = new int[subCount];
int rescount = endIndex - beginIndex + 1;
int[] scope = new int[rescount];
for (int i = beginIndex; i <= endIndex; i++)
scope[i - beginIndex] = i;
Random random = new Random();
for (int i = 0, odd = rescount - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = scope[ranindex];
scope[ranindex] = scope[odd];
}
return rs;
}
/**
* 获取一个集合列表指定数目的子集
*
* @param list 集合列表
* @param subCount 子集数目
* @return 子集
*/
public static List randomSubset(final List list, final int subCount) {
if (list == null || list.size() == 0 || list.size() < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
List rs = new ArrayList(subCount);
for (int i = 0; i < subCount; i++) {
rs.add(null);
}
Random random = new Random();
for (int i = 0, odd = list.size() - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs.set(i, list.get(ranindex));
list.set(ranindex, list.get(odd));
}
return rs;
}
/**
* 获取一个long型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static long[] randomSubset(long[] array, int subCount) {
long[] rs = new long[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 获取一个int型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static int[] randomSubset(int[] array, int subCount) {
int[] rs = new int[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
/**
* 获取一个Object型数组中指定数目的子集
*
* @param array 数组
* @param subCount 子集数目
* @return 子集
*/
public static Object[] randomSubset(Object[] array, int subCount) {
Object[] rs = new Object[subCount];
if (array == null || array.length == 0 || array.length < subCount) {
throw new RuntimeException("获取随机子集的参数不合逻辑!");
}
Random random = new Random();
for (int i = 0, odd = array.length - 1; i < subCount; i++, odd--) {
int ranindex = random.nextInt(odd);
rs[i] = array[ranindex];
array[ranindex] = array[odd];
}
return rs;
}
}
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/317297
随机子集的Java实现
JAVA基础:随机整数的生成
JAVA上加密算法的实现
observer设计模式的java实现
JBI-Java 实现 SOA 的标准途径
JAVA上加密算法的实现用例
java屏幕取词的实现
java屏幕取词的实现a
JavaScript escape/unescape编码的Java实现
java屏幕取词的实现
Java“多继承”实现的探究
JAVA上加密算法的实现用例
JAVA上加密算法的实现用例
java线程超时控制的实现
MySQL实现表中取出随机数据
ORACLE里面怎么实现随机循环
ubuntu 实现java开发
ubuntu 实现java开发
observer设计模式的java实现 - 小小冰的专栏
ChinaUnix.net - (ZT)JAVA上加密算法的实现用例
Java开源Blog实现,Roller 2.0 的安装手记!
JBI-Java 实现 SOA 的标准途径(翻译)
java实现的18位身份证格式验证算法
转贴:利用JAVA语言实现支持视频点播的WEB服务器