import java.util.HashSet; import java.util.Set;public class Random7 { public static void main(String[] args) { Set<Integer> results = new HashSet<Integer>(); int domain = 10000; while (results.size() < 7){ results.add(new Integer((int)(Math.random()*domain))); } for (int i : results) System.out.println(i); } }
hehe 老师说了不会做的话 就 问电脑的 呵呵 所以就求救各位高手
楼主很直白,鼓励诚实,但以后要努力学习,为中国崛起而读书!!!import java.util.*;public class Test { public static void main(String[] args) { // rand用来产生随机数 Random rand = new Random(System.currentTimeMillis());
final int len = 7; // 要产生的随机数个数 int index = 0; int[] numbers = new int[len]; // 存储产生的随机数
do { int n = rand.nextInt(1000); if (!Test.isIn(numbers, n)) { // 如果新产生的随机数不在数组里,加入数组里. numbers[index++] = n; } } while (index < len);
System.out.println(Arrays.toString(numbers)); }
// 判断一个数是否已经在指定的数组中,如在,返回true, 不在返回false. public static boolean isIn(int[] ns, int n) { for (int i = 0; i < ns.length; ++i) { if (ns[i] == n) { return true; } }
return false; } }
你个意思只能用数组咯。3楼上那个用的是集合,Set是一个不可以重复的集合。我帮你下个用数组的实现。测试正确,版主回去看看。 import java.util.Random;public class Test2 { public static void main(String args[]) { int[] a = new int[7]; boolean isSame = false; int count = 0; Random r = new Random(); while(count < 7) { isSame = false; int t = r.nextInt(10); for(int i = 0; i < count; i ++) { if(a[i] == t) { isSame = true; } } if(!isSame) { a[count] = t; count ++; } }
public static void main(String[] args) { int[] arr = new int[7]; int[] nums = new int[10]; Random r = new Random(); for (int i = 0; i < 10; i++) nums[i] = i; for (int i = 0; i < 7; i++) { int j = 0; while ((nums[j = r.nextInt(10)]) == 0) ; arr[i] = nums[j]; nums[j] = 0; } System.out.println(Arrays.toString(arr)); }
9L的方法可以解决问题,但是每次随机到一个已经被挑选过的数据,这次随机就会失效并重新随机,这个过程未免有点浪费了。这么说吧,如果随机的数据个数占总数的百分比也大,这个方法的效率也就也低。假如有0--1000个数据,分别从中随机500,700,900个不重复的数据,对9L的方法做了简单的测试如下: 随机个数/总数 500/1000 700/1000 900/1000 调用nextInt的平均随机次数 693 1203 2308当然对于从1000个数据里面随机900个数,可以反过来算随机100个,然后在从总数里面减去。我有一个办法,随机次数与需要随机的数据量相等:public class Test { public static int[] produce(int[] array,int num){ int[] result =new int[num]; int arrLen=array.length; Random rand = new Random(System.currentTimeMillis()); for(int i=0;i<num;i++){ int pos=rand.nextInt(arrLen-i); //随机一个位置 result[i]=array[pos]; array[pos]=array[arrLen-i-1]; //把该位置上的数据交换掉 }
import java.util.Set;public class Random7 {
public static void main(String[] args) {
Set<Integer> results = new HashSet<Integer>();
int domain = 10000;
while (results.size() < 7){
results.add(new Integer((int)(Math.random()*domain)));
}
for (int i : results) System.out.println(i);
}
}
public static void main(String[] args) {
// rand用来产生随机数
Random rand = new Random(System.currentTimeMillis());
final int len = 7; // 要产生的随机数个数
int index = 0;
int[] numbers = new int[len]; // 存储产生的随机数
do {
int n = rand.nextInt(1000);
if (!Test.isIn(numbers, n)) {
// 如果新产生的随机数不在数组里,加入数组里.
numbers[index++] = n;
}
} while (index < len);
System.out.println(Arrays.toString(numbers));
}
// 判断一个数是否已经在指定的数组中,如在,返回true, 不在返回false.
public static boolean isIn(int[] ns, int n) {
for (int i = 0; i < ns.length; ++i) {
if (ns[i] == n) {
return true;
}
}
return false;
}
}
import java.util.Random;public class Test2 { public static void main(String args[]) {
int[] a = new int[7];
boolean isSame = false;
int count = 0;
Random r = new Random();
while(count < 7) {
isSame = false;
int t = r.nextInt(10);
for(int i = 0; i < count; i ++) {
if(a[i] == t) {
isSame = true;
}
}
if(!isSame) {
a[count] = t;
count ++;
}
}
for(int s : a) {
System.out.println(s);
}
}
}
这不就是和彩票一样了么,从33个中选7个。这7个不重复就行是吧?如果是这样参照我http://topic.csdn.net/u/20100724/17/9d6537ab-0b17-470a-9205-6a3085e2dc60.html?75403这个帖子的回答!
System.out.println(s);
}
}
}这S:a 是什么意思啊
System.out.println(s);
}
}
}
这是个遍历 就是将数组a种的所有元素都输出
就相当于
for(int x = 0;x < a.length;a++){
System.out.println(a[x]);
}
public static List<Integer> list = new ArrayList<Integer>();
public static void main(String[] args) throws ParseException {
for (int i = 0; i < 7; i++) {
list.add(new Integer((int)(Math.random()*10)));
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}}
import java.util.Set;
public class haha {
public static Set<Integer> list = new HashSet<Integer>();
public static void main(String[] args) throws Exception {
for (int i = 0; i < 7; i++) {
list.add(new Integer((int)(Math.random()*1000)));
}
for (int i : list) {
System.out.print(i + " ");
}
}}
int[] arr = new int[7];
int[] nums = new int[10];
Random r = new Random();
for (int i = 0; i < 10; i++)
nums[i] = i;
for (int i = 0; i < 7; i++) {
int j = 0;
while ((nums[j = r.nextInt(10)]) == 0)
;
arr[i] = nums[j];
nums[j] = 0;
}
System.out.println(Arrays.toString(arr));
}
随机个数/总数 500/1000 700/1000 900/1000
调用nextInt的平均随机次数 693 1203 2308当然对于从1000个数据里面随机900个数,可以反过来算随机100个,然后在从总数里面减去。我有一个办法,随机次数与需要随机的数据量相等:public class Test {
public static int[] produce(int[] array,int num){
int[] result =new int[num];
int arrLen=array.length;
Random rand = new Random(System.currentTimeMillis());
for(int i=0;i<num;i++){
int pos=rand.nextInt(arrLen-i); //随机一个位置
result[i]=array[pos];
array[pos]=array[arrLen-i-1]; //把该位置上的数据交换掉
}
return result;
}
public static void main(String[] args){
int[] a=new int[1000];
for(int i=0;i<1000;i++)
a[i]=i+1;
int[] produced=produce(a,8);
for(int p:produced)
System.out.println(p);
}
}
int [] user = new int[7];
int i , j , count;
i = 0;
do
{
cai[i] = (int)(Math.random() * 35 + 1); //产生一个1-35随机数存入数组
//查询此数是否已经出现在前面的元素中
find = false;
for(j = 0; j < i; j++)
{
if(cai[i] == cai[j])
{
find = true;
break;
}
}
if(find == false) //如果没有出现,则继续下一个数
i++;
if(i == cai.length) //产生了7个数后即可退出循环
break;
}while(true);