62,616
社区成员
发帖
与我相关
我的任务
分享
Random random=new Random();
int[] res = new int[7];
int[] nums = new int[36];
for (int i = 0; i < 36; i++) {
nums[i] = i + 1;
}
for (int i = 0; i < 7; i++) {
int ran = random.nextInt(36 - i);
res[i] = nums[ran];
if (ran != 35 - i) {
nums[ran] ^= nums[35 - i];
nums[35 - i] ^= nums[ran];
nums[ran] ^= nums[35 - i];
}
}
Random random=new Random();
Set<Integer> r=new HashSet<Integer>(7);
while(r.size()<7){
int temp=random.nextInt(36)+1;
if(!r.contains(temp))
r.add(temp);
}
import java.util.Random;
public class Test {
public static void main(String[] args) throws Exception {
int[] a = new int[36];
int[] b = new int[7];
for(int i = 0;i<a.length;i++){
a[i]=i+1;
}
Random r = new Random();
for(int i=0;i<7;i++){
int x=r.nextInt(36-i);
b[i] = a[x];
int t = a[x];
a[x]=a[a.length-1-i];
a[a.length-1-i]=t;
}
for(int x :b){
System.out.print(x+" ");
}
}
}
import java.util.Random;
import java.util.List;
import java.util.*;
/**
* 随机访问一个数组中的所有元素一次且仅一次。采用模板方法模式,自定义实现访问
* 数组中元素的方法visit。需要用一个类继承RandomAccessOnce并且实现visit方法。
* @author chosen0ne
* 2011-01-11
*/
abstract class RandomAccessOnce<T>
{
public static final Random RANDOM_GEN=new Random();
/**
* 随机访问a[s..e]中所有元素一次且仅一次
* @param a T[] 待访问的集合
* @param s int 在a中访问的开始下标
* @param e int 在a中访问的结束下标
*/
public void randomAccessOnce(T[] a,int s,int e){
if(s<=e){
//在a[s..e]中随机选择一个元素,作为划分的基准,并且访问该元素
int k=RANDOM_GEN.nextInt(e-s+1)+s;
this.visit(a[k]);
//通过递归随机访问a[s..k-1]和a[k+1..e],这样防止了a[k]的再次访问
randomAccessOnce(a,s,k-1);
randomAccessOnce(a,k+1,e);
}
}
/**
* 随机访问a中所有元素一次且仅一次
* @param a T[] 待访问的集合
*/
public final void randomAccessOnce(T[] a){
randomAccessOnce(a,0,a.length-1);
}
/**
* 随机访问a[s..e]中所有元素一次且仅一次
* @param list List<T> 待访问的集合
* @param s int 在a中访问的开始下标
* @param e int 在a中访问的结束下标
*/
public void randomAccessOnce(List<T> list,int s,int e){
if(s<=e){
//在a[s..e]中随机选择一个元素,作为划分的基准,并且访问该元素
int k=RANDOM_GEN.nextInt(e-s+1)+s;
this.visit(list.get(k));
//通过递归随机访问a[s..k-1]和a[k+1..e],这样防止了a[k]的再次访问
randomAccessOnce(list,s,k-1);
randomAccessOnce(list,k+1,e);
}
}
/**
* 随机访问a中所有元素一次且仅一次
* @param list List<T> 待访问的集合
*/
public void randomAccessOnce(List<T> list){
randomAccessOnce(list,0,list.size()-1);
}
/**
* 钩子方法,子类必须实现这个方法,以便可以对集合元素进行访问
* @param o T 要访问的集合
*/
public abstract void visit(T o);
public static void main(String[] arg){
System.out.println("use array..................");
Integer[] a={1,2,3,4,5,6,7,8,9,10};
RandomAccessOnce<Integer> r=new RandomAccessImpl<Integer>();
r.randomAccessOnce(a);
System.out.println("use List..................");
List<Integer> list=new LinkedList<Integer>();
for(Integer i:a)
list.add(i);
r.randomAccessOnce(list);
}
}
class RandomAccessImpl<T> extends RandomAccessOnce<T>
{
public void visit(T o){
System.out.println("play cd: "+o);
}
}
Random random=new Random();
int [] r=new int[7];
for (int i = 0; i < r.length;) {
int temp=random.nextInt(36);
if(temp==0)continue;
for (int j : r) {
if(j==temp)continue;
}
r[i]=temp;
i++;
}
HashSet<Integer> set = new HashSet<Integer>();
Random ran = new Random();
while(set.size() < 7){
set.add(ran.nextInt(36) + 1);
}