int[] int_array = new int[10000]; Random random = new Random(); for(int i =0;i<10000; i++){ boolean b = true; int k = random.nextInt(9000)+1000; for(int j = 0; j<int_array.length;j++){ if(k==int_array[j]){ b=false; } } if(b){ int_array[i] = k; } }
public static void main(String[] args) { Set set=new HashSet(); while(set.size()<10000){ int val=(int) (Math.random()*10000+1000); set.add(val); }
System.out.println("随机产生数字的个数:"+set.size()); for (Iterator iterator=set.iterator();iterator.hasNext();) { System.out.println(iterator.next()); }
0000-9999刚好1万个四位数字 所以可以把这1万个放到集合中,每次随机从集合取走一个,取完后从集合删掉 for example//准备好1万个不同的四位数 List<String> list = new LinkedList<String>(); for (int i=0; i<10000; i++) { list.add(String.format("%04d", i)); } //随机取1万次不同的四位数 for (int i=0; i<10000; i++) { int index = (int)(Math.random()*list.size()); System.out.printf("random 4 number: %d\n", list.remove(index)); }
上万个四位数用double还是有的.不知可不可以这样 public class RandomTest {
@Test public void test01() { Set set=new HashSet(); while(set.size()<10000){ double val=(Math.random()*10000+1000); set.add(val); } System.out.println("随机产生数字的个数:"+set.size()); for (Iterator iterator=set.iterator();iterator.hasNext();) { System.out.println(iterator.next()); } }
}
for (int i=0; i<10000; i++) { int index = (int)(Math.random()*list.size()); System.out.printf("random 4 number: %s\n", list.remove(index)); //笔误了,把这里改成%s就可以了 }
//我用的是C,根据取一个,去一个的原则 #include <stdio.h> #include <stdlib.h > #define yu 10000 int bishi(int wby[],int a,int sum) { int i=0; for (i=a;i<(yu-sum-1);i++) { wby[i]=wby[i+1]; } return 0; } int main() { int wby[yu]; int hzy[yu]; int i; int s; for(i=0;i<yu;i++) { hzy[i]=i; } for(i=0;i<yu;i++) { s=rand()%(yu-i); wby[i]=hzy[s]; bishi(hzy,s,i); printf("%d_",s); } for(i=0;i<yu;i++) { printf("%d\n",wby[i]); } }
class Random { public: Random(int s,int e); virtual ~Random();
private: int* pMem;//存放区间所有随机数 int n;//当前已使用的随机数个数 int total;//区间数总数 public: int getValue();}; Random::Random(int s,int e):pMem(NULL) { total=e-s;//区间总数 pMem=new int[total-1]; for(int i=0;i<total;i++)//数组赋值 pMem[i]=s+i; n=0; } Random::~Random() { if(pMem) { delete pMem; } } int Random::getValue() { if(n == total)//全部使用过了,直接返回-1 return -1; int i=rand()%(total - n);//剩余数组随机产生下标 int r=pMem[i]; for(int j=i;j<total;j++)//将后面的数移动1位,即消除已经使用的索引为i的随机数 pMem[j] =pMem[j+1]; n++; return r; }int main(int argc, char* argv[]) { Random r(1000,9999); for(int i=0;i<40;i++) cout<<r.getValue()<<endl; return 0; }
class Random { public: Random(int s,int e); virtual ~Random();
private: int* pMem;//存放区间所有随机数 int n;//当前已使用的随机数个数 int total;//区间数总数 public: int getValue();}; Random::Random(int s,int e):pMem(NULL) { total=e-s;//区间总数 pMem=new int[total-1]; for(int i=0;i<total;i++)//数组赋值 pMem[i]=s+i; n=0; } Random::~Random() { if(pMem) { delete pMem; } } int Random::getValue() { if(n == total)//全部使用过了,直接返回-1 return -1; int i=rand()%(total - n);//剩余数组随机产生下标 int r=pMem[i]; for(int j=i;j<total;j++)//将后面的数移动1位,即消除已经使用的索引为i的随机数 pMem[j] =pMem[j+1]; n++; return r; }int main(int argc, char* argv[]) { Random r(1000,9999);//随便设置一个区间,随机数将从其中产生 for(int i=0;i<40;i++) cout<<r.getValue()<<endl; return 0; }
static int i=0;public String getRandom(){ String r = i++; while(r.lenth<4){ r="0"+r; } return r; }
Random random = new Random();
for(int i =0;i<10000; i++){
boolean b = true;
int k = random.nextInt(9000)+1000;
for(int j = 0; j<int_array.length;j++){
if(k==int_array[j]){
b=false;
}
}
if(b){
int_array[i] = k;
}
}
Set set=new HashSet();
while(set.size()<10000){
int val=(int) (Math.random()*10000+1000);
set.add(val);
}
System.out.println("随机产生数字的个数:"+set.size());
for (Iterator iterator=set.iterator();iterator.hasNext();) {
System.out.println(iterator.next());
}
}
如果0可以加在前面的话有这么多,0不能加前面那我怀疑lz的zs了
所以可以把这1万个放到集合中,每次随机从集合取走一个,取完后从集合删掉
for example//准备好1万个不同的四位数
List<String> list = new LinkedList<String>();
for (int i=0; i<10000; i++) {
list.add(String.format("%04d", i));
}
//随机取1万次不同的四位数
for (int i=0; i<10000; i++) {
int index = (int)(Math.random()*list.size());
System.out.printf("random 4 number: %d\n", list.remove(index));
}
public class RandomTest {
@Test
public void test01()
{
Set set=new HashSet();
while(set.size()<10000){
double val=(Math.random()*10000+1000);
set.add(val);
} System.out.println("随机产生数字的个数:"+set.size());
for (Iterator iterator=set.iterator();iterator.hasNext();) {
System.out.println(iterator.next());
} }
}
for (int i=0; i<10000; i++) {
int index = (int)(Math.random()*list.size());
System.out.printf("random 4 number: %s\n", list.remove(index)); //笔误了,把这里改成%s就可以了
}
#include <stdio.h>
#include <stdlib.h >
#define yu 10000
int bishi(int wby[],int a,int sum)
{
int i=0;
for (i=a;i<(yu-sum-1);i++)
{
wby[i]=wby[i+1];
}
return 0;
}
int main()
{
int wby[yu];
int hzy[yu];
int i;
int s;
for(i=0;i<yu;i++)
{
hzy[i]=i;
}
for(i=0;i<yu;i++)
{
s=rand()%(yu-i);
wby[i]=hzy[s];
bishi(hzy,s,i);
printf("%d_",s);
}
for(i=0;i<yu;i++)
{
printf("%d\n",wby[i]);
}
}
class Random
{
public:
Random(int s,int e);
virtual ~Random();
private:
int* pMem;//存放区间所有随机数
int n;//当前已使用的随机数个数
int total;//区间数总数
public:
int getValue();};
Random::Random(int s,int e):pMem(NULL)
{
total=e-s;//区间总数
pMem=new int[total-1];
for(int i=0;i<total;i++)//数组赋值
pMem[i]=s+i;
n=0;
}
Random::~Random()
{
if(pMem)
{
delete pMem;
}
}
int Random::getValue()
{
if(n == total)//全部使用过了,直接返回-1
return -1;
int i=rand()%(total - n);//剩余数组随机产生下标
int r=pMem[i];
for(int j=i;j<total;j++)//将后面的数移动1位,即消除已经使用的索引为i的随机数
pMem[j] =pMem[j+1];
n++;
return r;
}int main(int argc, char* argv[])
{
Random r(1000,9999);
for(int i=0;i<40;i++)
cout<<r.getValue()<<endl;
return 0;
}
class Random
{
public:
Random(int s,int e);
virtual ~Random();
private:
int* pMem;//存放区间所有随机数
int n;//当前已使用的随机数个数
int total;//区间数总数
public:
int getValue();};
Random::Random(int s,int e):pMem(NULL)
{
total=e-s;//区间总数
pMem=new int[total-1];
for(int i=0;i<total;i++)//数组赋值
pMem[i]=s+i;
n=0;
}
Random::~Random()
{
if(pMem)
{
delete pMem;
}
}
int Random::getValue()
{
if(n == total)//全部使用过了,直接返回-1
return -1;
int i=rand()%(total - n);//剩余数组随机产生下标
int r=pMem[i];
for(int j=i;j<total;j++)//将后面的数移动1位,即消除已经使用的索引为i的随机数
pMem[j] =pMem[j+1];
n++;
return r;
}int main(int argc, char* argv[])
{
Random r(1000,9999);//随便设置一个区间,随机数将从其中产生
for(int i=0;i<40;i++)
cout<<r.getValue()<<endl;
return 0;
}
String r = i++;
while(r.lenth<4){
r="0"+r;
}
return r;
}