我想随机1到50的随机数字。Random rand = new Random();int i = rand.NextInt(50);如果我生成10次数字5之后,就不要再生成数字5了,这样该怎么办?加判断?if(int 生成>5)
{
int i = rand.NextInt(50);
}等待回复。同时我也自己来,看谁做的快点。
{
int i = rand.NextInt(50);
}等待回复。同时我也自己来,看谁做的快点。
思路在这,没测,你测一下:
import java.util.Random;public class TestMain {
static int SIZE = 100;
public static void main(String[] args) {
Random r = new Random();
int[] randomNums = new int[SIZE];
int num = 0;
for(int i = 0; i < SIZE; i++) {
int randomNum = r.nextInt(50);
if(num == 10) {
while(randomNum == 5) {
randomNum = r.nextInt(50);
}
randomNums[i] = randomNum;
} else {
if(randomNum == 5) {
num++;
randomNums[i] = randomNum;
}
}
}
}
}
// TODO Auto-generated method stub int[] result = new int[100];//保存结果 Random rand = new Random();//调用随机类 int[] temp = new int[50]; //生成一个临时数组 判断每个数字是否超过两次出现 for(int i = 0; i < 100; i++)
{
int x = rand.nextInt(50);//生成一个0到50的随机数字 if(temp[x]<2){//如小与2,也就是少于两次出现,那么保存到Result[]里面 //System.out.println(temp[result[i]]+" kao");//忽略这行 result[i] = x;保存结果 temp[result[i]]++;}//因为出现了一次,所以这个temp+1 else{//如果大于2 我就想换个数字生成,可是怎么都写不好
System.out.println("oh..");
} System.out.println(i+" "+result[i]+" "+temp[i]);//结果这里还出现个错误。哎
}
}
import java.util.Random;public class TestMain {
static int SIZE = 100;
public static void main(String[] args) {
Random r = new Random();
int[] randomNums = new int[SIZE];
int num = 0;
for(int i = 0; i < SIZE; i++) {
int randomNum = r.nextInt(50);
if(num == 10) {
while(randomNum == 5) {
randomNum = r.nextInt(50);
}
randomNums[i] = randomNum;
} else {
if(randomNum == 5) {
num++;
}
randomNums[i] = randomNum;
}
}
}
}
else{
while(temp[x]<2){
x = rand.nextInt(50);//继续生成,直到满足你说的条件为止。
}
}
你的意思是不是5出现十次就不循环了?其它产生的数,放到一个数组里吧?估且这样理解public class Test {
public static void main(String[] args) {
Random rand = new Random();
int count = 0;
List<Integer> list = new LinkedList<Integer>();
for (;;) {
int i = rand.nextInt(50);
if (i == 5) {
count++;
if (count == 10) {
break;
}
} else {
list.add(i);
}
} System.out.println(list);
}
}
20 0
23 0
2 0
14 0
26 0
12 0
19 0
41 0
1 0
43 0
28 0
19 0
18 0
34 0
13 0
38 0
20 0
42 0
6 0
7 0
结果根本不对。你的num没有记录次数。哎。。
for(int i = 0; i < 200; i += 2) {
Integer value = new Integer(i / 2);
b.add(value);
b.add(value);
}
Collections.shuffle(b);
int[] result = new int[200];
for(int i = 0; i < result.length; i++)
result[i] = b.get(i);
System.out.println(Arrays.toString(result));
int[] result = new int[200];
for(int i = 0; i < 200; i += 2)
result[i] = result[i + 1] =i / 2;
Random rand = new Random();
for(int i = 200; i > 1; i--) {
int change = rand.nextInt(i);
int tmp = result[i - 1];
result[i - 1] = result[change];
result[change] = tmp;
}
System.out.println(Arrays.toString(result));
int randCountDone = 0;
int[] randResultArr = new int[randCount];
int[] tempResult = new int[100];
// 初始化临时数组
for(int l = 0; l < 100; l++) {
tempResult[l] = -1;
}
Random rand = new Random();
tempResult[0] = rand.nextInt(50);
randResultArr[0] = tempResult[0];
randCountDone = 1;
while(randCountDone < randCount) {
tempResult[randCountDone] = rand.nextInt(50);
int duplicateCount = 0;
for(int k = 0; k < tempResult.length; k++) {
if(tempResult[randCountDone] == tempResult[k]) {
duplicateCount++;
}
}
if(duplicateCount <= 2) {
randResultArr[randCountDone] = tempResult[randCountDone];
randCountDone++;
}
}
//Arrays.sort(randResultArr);
for(int j : randResultArr) {
System.out.println(j);
}
你看看是不是你要的逻辑
public class RandomTest {
private static Integer[] result = new Integer[200]; public static void main(String[] args) {
Random r = new Random();
for(int i=0;i<100;i++)
for(int n=0;n<2;n++)
while(true) {
int pos = r.nextInt(200);
if(result[pos] == null) {
result[pos] = new Integer(i);
break;
}
} for(int i=0;i<200;i++) System.out.println(result[i]); //Print result
}
}
public static int[] temps = new int[50];
public static void main(String[] args) {
int[] result = new int[100];// 保存结果 Random rand = new Random();// 调用随机类 // 生成一个临时数组 判断每个数字是否超过两次出现
int i = 0;
for (;;) {
int x = rand.nextInt(50);// 生成一个0到50的随机数字 if (temps[x] < 2) {// 如小与2,也就是少于两次出现,那么保存到Result[]里面 // System.out.println(temp[result[i]]+" kao");//忽略这行
temps[x]=temps[x]+1;
result[i] = x;// 保存结果
System.out.println(i+" :"+result[i]);
i++;
if(over()){
break;
}
// temp[result[i]]++;
}// 因为出现了一次,所以这个temp+1 else {// 如果大于2 我就想换个数字生成,可是怎么都写不好
// System.out.println("oh..");
} // System.out.println(i+" "+result[i]+" "+temp[i]);//结果这里还出现个错误。哎
} }
public static boolean over(){
boolean temp=true;
for(int i=0;i<50;i++){
if(temps[i]!=2){
temp=false;
break;
}
}
return temp;
}
}
import java.util.Random;public class Test {
public static int[] temps = new int[100];
public static void main(String[] args) {
int[] result = new int[200];// 保存结果
Random rand = new Random();// 调用随机类
int i = 0;
for (;;) {
int x = rand.nextInt(100);// 生成一个0到50的随机数字 if (temps[x] < 2) {// 如小与2,也就是少于两次出现,那么保存到Result[]里面
temps[x]=temps[x]+1;
result[i] = x;// 保存结果
System.out.println(i+" :"+result[i]);
i++;
if(over()){//判断是否,都随机好了,每个都两次
break;
}
}// 因为出现了一次,所以这个temp+1 else {// 如果大于2 我就想换个数字生成,可是怎么都写不好
}
} }
public static boolean over(){//判断是否,都随机好了,每个都两次
boolean temp=true;
for(int i=0;i<100;i++){
if(temps[i]!=2){
temp=false;
break;
}
}
return temp;
}
}感觉太乱,顺便改成,你的要,200个数,还是在你的代码基础上改下
<Integer>
// TODO Auto-generated method stub int[] result = new int[100];//保存结果 Random rand = new Random();//调用随机类 int[] temp = new int[50]; //生成一个临时数组 判断每个数字是否超过两次出现 generateRandom(result, rand, temp);
System.out.println(i+" "+result[i]+" "+temp[i]);//结果这里还出现个错误。哎
}
}public int generateRandom(int[] result, Random rand, int[] temp){
for(int i = 0; i < 100; i++)
{
int x = rand.nextInt(50);//生成一个0到50的随机数字 if(temp[x]<2){//如小与2,也就是少于两次出现,那么保存到Result[]里面 //System.out.println(temp[result[i]]+" kao");//忽略这行 result[i] = x;保存结果 temp[result[i]]++; break;
}//因为出现了一次,所以这个temp+1 else{
generateRandom(result, rand, temp); // 这里调用自身
}
}试试这样行不行
i=从199递减到0。程序自己写吧。
---->public void generateRandom(int[] result, Random rand, int[] temp){ 不知道有没有解决你的问题
import java.util.Random;
public class TestMain {
public static void main(String[] args){
int[] randomNums = new int[200];//存放200个数
int[] flags = new int[100];//标记当前随机数被存放的次数
Random r = new Random(System.currentTimeMillis());;
for(int i = 0; i < 200; i++) {
int randomNum = r.nextInt(100);
if(flags[randomNum] == 2){//如果已经存放两次
while(flags[randomNum] == 2) {//重新生成随机数,直到找到存放次数少于2次的数
randomNum = r.nextInt(100);
}
randomNums[i] = randomNum;//存放随机数
} else {//如果存放还没到两次
flags[randomNum] = flags[randomNum] + 1;//把存放当前数字的标记位+1
randomNums[i] = randomNum;//直接存放
}
}
for(int i = 0; i < 200; i++) {
System.out.println(randomNums[i]);
}
}
}
// 随机数下限
/// 随机数上限
public int[] GetRandomArray(int Number,int minNum,int maxNum)
{
int j;
int[] b=new int[Number];
Random r=new Random();
for(j=0;j<Number;j++)
{
int i=r.Next(minNum,maxNum+1);
int num=0;
for(int k=0;k<j;k++)
{
if(b[k]==i)
{
num=num+1;
}
}
if(num==0 )
{
b[j]=i;
}
else
{
j=j-1;
}
}
return b;
}
参考
参考
int[] result = new int[100]; //保存结果
Random rand = new Random(); //调用随机类
int[] temp = new int[50]; //生成一个临时数组 判断每个数字是否超过两次出现 for(int i = 0; i < 100; i++)
{
int x = rand.nextInt(50); //生成一个0到50的随机数字 while(temp[x] >= 2){
x = rand.nextInt(50);
} result[i] = x; //保存结果
temp[x] ++; //因为出现了一次,所以这个temp+1 System.out.println(i + " " + result[i] + " " + temp[x]); //temp[i],当i=50数组就越界了
}
package com.ricky.www;import java.util.ArrayList;
import java.util.Random;public class CreateRandomArray {
private static int[] resultArray;
private static int[] metaArray;
private final static int appearTimes = 3; public static void main(String[] args) {
init(50);
int[] array = createRandomArray();
for (int i = 0; i < array.length; i++) {
if (i % 10 == 0) {
System.out.println();
}
System.out.printf("%-6d", array[i]);
}
} public static void init(int metaArrayLength) {
resultArray = new int[metaArrayLength * appearTimes];
metaArray = new int[metaArrayLength]; for (int i = 0; i < metaArray.length; i++) {
metaArray[i] = i;
}
} public static int[] remove(int[] array, int element) {
ArrayList<Integer> list = new ArrayList<Integer>();
Integer[] iArray = new Integer[array.length];
int index = 0;
for (int value : array) {
iArray[index++] = value;
}
for (Integer value : iArray) {
list.add(value);
}
list.remove(new Integer(element));
int[] result = new int[array.length - 1];
index = 0;
for (Integer value : list) {
result[index++] = value;
}
return result;
} public static int[] createRandomArray() {
int times[] = new int[metaArray.length];
for (int i = 0; i < times.length; i++) {
times[i] = 0;
}
Random random = new Random();
int index = 0;
for (int i = 0; i < resultArray.length; i++) {
int temp = random.nextInt(metaArray.length);
times[metaArray[temp]]++;
resultArray[index++] = metaArray[temp];
if (times[metaArray[temp]] == appearTimes) {
metaArray = remove(metaArray, metaArray[temp]);
}
}
return resultArray;
}
}这是我写的,虽然当出现次数达到appearTimes的时候都需要进行一次数组的调整(一定得运行时间),但是总的来说会比不断的得到有可能不能直接写入的数据快些。
// 1-50的数添加到 ArrayList 2次。(如果要判断不超过10次就添加10次)
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=1; i<=50; i++) {
for(int j=0; j<2; j++)
list.add(i);
}
// 填充数组 (生成无重复随机数的方法)
int[] arr = new int[100];
for(int i=0; i<100; i++) {
arr[i] = list.remove((int)(Math.random()*list.size()));
}
// 打印数组
for(int i=0; i<100; i++) {
System.out.print(arr[i] + " ");
if((i+1)%10 == 0)
System.out.println();
}
0[0] = 0; 0[1] = 0;
...
...
...
100[0] = 100; 100[1] = 100; 这样这个数组就保存了2*100个整数,这里面就是我想要的全部。
然后就是遍历这个数组2*100依次从0.0走到100.1,每次都看Result是否为空(我设置Value = 9999为空)。死循环:{是空的,就把2*100数组的当前一个元素放进去;Break}
20L的程序我给你解释一下,开始是搞一个list,放上200个数字(100个数字,每个放2次),然后 洗牌Collections.shuffle(b); 200个数就随机分布了,最后list放进数组里
ltandfyy
zhuyx808
seaman_xh ...朋友。我还要继续研究J2se。书看完了,真正做Project的时候,发现自己差的地方很多。
就算不用API中的shuffle也绝对不是你的方法最简单,而是我在第21楼的方法最简单。你的方法在最后几轮的时候会浪费大量的时候重复生成一个可以使用的随机数,大部分的随机数都会被抛弃(因为绝大部分的result[pos]已经不为null了)。