生成一个随机的不重复4位数

Tang_huawei 2012-02-21 11:46:22
生成一个随机的4位数,并且每一位都不重复,用三种方法
...全文
1302 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyp907093825 2012-02-22
  • 打赏
  • 举报
回复
public static void main(String[] args) {

Random rand=new Random();
List<Integer> set=new ArrayList<Integer>();
for(int i=0;i<10;i++){set.add(new Integer(i));}

int beishu=1;
int result=0;

for(int i=0;i<4;i++){

Integer in= set.get(rand.nextInt(9-i));
set.remove(in);
result+=in*beishu;
beishu*=10;
if(i==2){set.remove(new Integer(0));}
}
System.out.println(result);


}
lyp907093825 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lyp907093825 的回复:]

我这个测试过的啦,没问题的

package com.gzsoft.zoom;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class TestSIWEIS……
[/Quote]
错了,set.add(0);
//这行要注释掉的!
lyp907093825 2012-02-22
  • 打赏
  • 举报
回复
我这个测试过的啦,没问题的

package com.gzsoft.zoom;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class TestSIWEISHU {


public static void main(String[] args) {
Random rand=new Random();
List set=new ArrayList();
set.add(0);
for(int i=0;i<10;i++){
set.add(new Integer(i));
}


int beishu=1;
int result=0;

for(int i=0;i<4;i++){
int ranNum=rand.nextInt(9-i);
Integer in=(Integer) set.get(ranNum);
set.remove(in);
result+=in*beishu;
beishu=beishu*10;
if(i==2){
set.remove(new Integer(0));
}
}
System.out.print(result);

}

}
ilovefzq 2012-02-22
  • 打赏
  • 举报
回复
方法2 public static void main(String[] args) {

{
while (true) {
int a = (int) (Math.random() * 10000 + 1000);

if (a > 9999)
continue;
else {
int[] b = new int[] { (a / 1000),
(a / 100) - (a / 1000) * 10,
((a % 100) - a % 10) / 10, a % 10 };
if (b[0] == b[1])
continue;
else if (b[0] == b[2])
continue;
else if (b[0] == b[3])
continue;
else if (b[1] == b[2])
continue;
else if (b[1] == b[3])
continue;
else if (b[2] == b[3])
continue;
else {
System.out.println(a);
break;
}

}
}

}
}
ilovefzq 2012-02-22
  • 打赏
  • 举报
回复
public static void main(String[] args) {

{
while (true) {
int a = (int) (Math.random() * 10000 + 1000);

if (a > 9999)
continue;
else {
int[] b = new int[] { (a / 1000),
(a / 100) - (a / 1000) * 10,
((a % 100) - a % 10) / 10, a % 10 };
if (b[0] == b[1])
continue;
else if (b[0] == b[2])
continue;
else if (b[0] == b[3])
continue;
else if (b[1] == b[2])
continue;
else if (b[1] == b[3])
continue;
else if (b[2] == b[3])
continue;
else {
System.out.println(a);
break;
}

}
}

}
}
莫欺少年穷 2012-02-22
  • 打赏
  • 举报
回复
用Random随机一个数r,然后另外三个数就是r+1、r+2和r+3行不?嘻嘻
神经斌 2012-02-22
  • 打赏
  • 举报
回复
更正一下:

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int result = 0;
Random random = new Random();
for (int i = 0, len = 9, index = 0; i < 4; i++) {
index = random.nextInt(len);
result = result * 10 + array[index];
array[index] = array[len--];
}
System.out.println("result -> " + result);

Random类选随机数的范围也是[ )样的。
神经斌 2012-02-22
  • 打赏
  • 举报
回复

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int result = 0;
Random random = new Random();
for (int i = 0, len = 8, index = 0; i < 4; i++) {
index = random.nextInt(len);
result = result * 10 + array[index];
array[index] = array[len-- + 1];
}
System.out.println("result -> " + result);
安特矮油 2012-02-22
  • 打赏
  • 举报
回复
1.定义一个数组{0,1,2,3,4.....9}从里面取,第一位不为0,用随机0~length的随机数来取
2.产生0~9的随机数,同样第一位不为0,需要进行重复比较,如果重复则重新取随机数
3.想不到了....
zqfddqr 2012-02-22
  • 打赏
  • 举报
回复
xyz0101123132 2012-02-22
  • 打赏
  • 举报
回复
产生均匀分布的算法
1、选择区域缩小法:
定义一个数组array{0,1,2,3,4,5,6,7,8,9},用Random(array.length)函数选择下标i,读取array[i]做第一位,移除array[i],再用Random(array.length-1)选择下标i,读取array[i]做第二位,移除array[i]依次类推。。
时间复杂度为 T(n) = O(4)
2、冲突检查规避法:
用Random(10),取出第一位r1,再用Random(10)取出第二位,判断是否等于第一位,是重新获取。否则做第2位依次类推
最好时间复杂度 T(n) = O(4)//无冲突的情况
平均时间复杂度 T(n) = O(4(1+1/4));
最坏时间复杂度 T(n) = O(无穷大)//每次都冲突的情况
由于Random函数产生的随机数是均匀分布的,所以最坏情况的出现率基本不会出现。
3、绝对值冲突检查调整法:
用Random(10),取出第一位r1,再用Random(10)取出与第二位的差值 x1 ,用(r1+x1)%10做第二位,再用Random(10)取出与第三位的差值 x2,看x2是否和x1相等如果是重新获取x2,否则用(r1+x2)%10做第三位,依次类推
时间复杂度同2;
4,定义数组array,遍历0000——9999检查两位是否相等如果每一位都不相等,填入到array,用Random(array.leng)获取结果
时间复杂度同 T(n) = O(10001)

产生非均匀分布的算法(这个比较扯)
1、用Random(10),取出第一位r1,(r1+1)%10做第二位,(r1+2)%10做第三位,(r1+3)%10做第四位
时间复杂度为 T(n) = O(1)
其他你可以发挥,不过这个比较扯。
luoiori 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sd4261829 的回复:]
Java code

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int result = 0;
Random random = new Random();
for (int i = 0, len = 8, index = 0; i < 4; i++) {
……
[/Quote]
我看了你“ array[index] = array[len-- + 1];”代码 我想为什么--又+1呢;为什么呢;为什么呢;

才发现还有4楼;哎,哎

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧