C语言关于产生几个不同随机数的问题,请大家帮忙解答一下

haitun_88 2009-03-28 02:34:54
如何在1-100这100个数同时产生10个不同的随机数,我只知道要用到srand()和random()函数 但是还是不会写 请高手解答一下
...全文
1705 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_new 2009-03-28
  • 打赏
  • 举报
回复
伪随即


float g_rand;
int g_rand_flag=0;



unsigned int rand_my(unsigned int rand_max,unsigned int rand_min)
{
float rand_result;

if(g_rand_flag==0)
{
g_rand_flag=1;
g_rand=(float)(int)(&rand_min)/(float)(int)(&rand_max);
}

if(g_rand==0 || g_rand==1)
{
g_rand += 8.739f;/*Ëæ±ãдµÄ*/
}

rand_result = g_rand * g_rand;

while(rand_result<1)
{
rand_result *= 10;
}

rand_result = rand_result - (float)((int)rand_result);

g_rand = rand_result==g_rand?8.732139f:rand_result;

return (unsigned)((rand_max-rand_min)*rand_result + rand_min);
}

void main()
{
int a[10];
int i,j;

memset(a,0xff,10*sizeof(int));

for(i=0;i<10;i++)
{
a[i] = rand_my(100,0);

for(j=i-1;j>=0;j--)
{
if(a[i]==a[j])
{
i--;
break;
}
}
}

}
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 softwaregf 的回复:]
rand 函数吧,如果你想学好,就自己看下面的内容吧,你就明白了,
srand()就是给rand()提供种子seed

1.>先看一个例子
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbe…
[/Quote]
可以
magipan 2009-03-28
  • 打赏
  • 举报
回复
额,有个错误
swap(a,j,n);应该是swap(a,j,n-1);
magipan 2009-03-28
  • 打赏
  • 举报
回复
采用8楼的方法,把选出来的数换到数组最后面去
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int arr[],int a,int b)
{
int temp;
temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
int main()
{
int a[100], i, j,n=100;
for(i=0;i<100;i++)//存上1~100的数
{
a[i]=i+1;
}
srand((unsigned)time(NULL));

for(i = 0; i < 10; ++i)
{
j=rand()%n;//要取的数的下标
swap(a,j,n);//换到后面去
n--;//下次取数的范围减一
}

for(i = 99; i > 89; --i) //最后面10个数就是要求的
printf("%d ", a[i]);
printf("\n");
system("pause");
return 0;
}

可以修改使得1~100乱序输出
localxiao 2009-03-28
  • 打赏
  • 举报
回复
补充下,随机数的上限每次减一
localxiao 2009-03-28
  • 打赏
  • 举报
回复
提个想法
创建一个线性表,依次放入1到100
然后通过rand()%100+1获得一个序号,取出线性表中对应的数
然后删除相应结点,可以做到不重复~
haitun_88 2009-03-28
  • 打赏
  • 举报
回复
我刚刚也用rand()函数写了一下 觉得还是和random一样只是google了一下 好像现在都不用random这个东西了因为很多编译工具不支持,但是我写了之后还是不知道如何实现不同的问题 请看看我下面的code 看看要怎么才能实现5个数都不同,我是新手,确实很多不懂 向大家学习了

#include <stdio.h> //头文件
#include<stdlib.h>//标准工具库,要用到其中的rand()
#include<time.h>//时间库,要用到里面的时间来做随机数的种子
#define random(x) (rand()%x)

main()
{


srand((int)time(0)); //利用时间作原子,这样第一次产生的随机数就不同了
int a=1 + rand() % 101;
int b=1 + rand() % 101;
int c=1 + rand() % 101;
int d=1 + rand() % 101;
int e=1 + rand() % 101;
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
printf("%d\n",e);
//printf("%d\n",random(100));//显示
system("pause");
}
elmnd 2009-03-28
  • 打赏
  • 举报
回复
#include <cstdlib>
#include <iostream>
#include <time.h>
#define N 11
//算10个数的不重复的随机数
using namespace std;

int main(int argc, char *argv[])
{
int a[N], i, j;
srand(time(0));

for(i = 0; i < N; ++i)
{
a[i] = rand()%N;
//判断
for(j = 0; (j<i)&&(i!=0); ++j)
{
if(a[i] == a[j])
{
a[i] = rand()%N;
j = 0; //重新判断
}
}
}

for(i = 1; i < N; ++i) //PS:有点问题, 就是会跟第一个数重复。所以要不算第一个数
printf("%d ", a[i]);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}

刚刚写的很烂的做法, 随即的数一多, 就死定了
haitun_88 2009-03-28
  • 打赏
  • 举报
回复
我研究了一晚上确实没有找到列子
我尝试写了一下用random()这个函数
但是这样就会有重复出现 现在想如何解决重复这个问题 列子如下
#include <stdio.h> //头文件
#include<stdlib.h>//标准工具库,要用到其中的rand()
#include<time.h>//时间库,要用到里面的时间来做随机数的种子
#define random(x) (rand()%x)

main()
{
int a=100,b=100,c=100,d=100,e=100;

srand((int)time(0)); //利用时间作原子,这样第一次产生的随机数就不同了
printf("%d\n",random(a));
printf("%d\n",random(b));
printf("%d\n",random(c));
printf("%d\n",random(d));
printf("%d\n",random(e));
printf("%d\n",random(100));//显示
system("pause");
}
yueludragon 2009-03-28
  • 打赏
  • 举报
回复
这个在csdn上查找一下就有例子的
haitun_88 2009-03-28
  • 打赏
  • 举报
回复
回复楼上的 我想请问一下rand 函数和random函数有什么区别呢?
woyaoxiazaiziliao 2009-03-28
  • 打赏
  • 举报
回复
srand(time(NULL));
rand()%100
softwaregf 2009-03-28
  • 打赏
  • 举报
回复
rand 函数吧,如果你想学好,就自己看下面的内容吧,你就明白了,
srand()就是给rand()提供种子seed

1.>先看一个例子
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

2.>关于time.h
time.h中包含很多有趣的函数,譬如
char *ctime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
字符串:Mon Nov 21 11:31:54 1983\n\0

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;


void main()
{
time_t t1,t2;
char getTime[20];
char *ptstring=getTime;

int x,count=0;
x=RAND_MAX;
cout<<<‘/n‘;
t1=time(NULL);
ptstring=ctime(&t1);

while(count<=100)
{
srand( (unsigned)time( NULL ) );
x=rand()%50;
if(x<5)
continue;
else
{
count++;
cout<<"the numth is "<<<‘\n‘;
}
}

查看ptstring的值会显示 "Tue Sep 13 16:31:06 2005"

3.>最后说说srand()函数
void srand(unsigned seed) 初始化随机数发生器

有讨论如下:
1.C的函数库之所以没有把使用系统时钟初始化随机种子这步重要的操作直接放进rand()函数的实现中,我觉得至少有三个原因:
(1)可以高效产生连续的随机数,不用每次都初始化;
(2)给程序员以更高的灵活性,因为可能在要求较高的场合,应该使用更好的的数据做种子,而不是系统时钟;
(3)对于只是想产生大量伪随机数来尽兴某种验证或者统计,未必需要初始化,大不了程序每次运行都产生同样的一系列随机数而已——有些情况下,这是无所谓的。

事实上有一个更重要的原因:
作为伪随机序列产生器的rand()函数,必须具备的一个重要特性就是-》产生的序列必须是可重现的。这不仅仅是一个算法,相当大的程度上,它关系到代码测试的准确性。如果算法中使用了和rand()的结果相关的数据,通过一个可控的可重现序列,我们就有机会再现每一次测试的过程,从而更有效的找到问题的所在。所以这里提出一个建议,代码中,如果rand()的函数结果关系到算法的结果,那么,必须保证你的rand()调用是可重现的。


4.>C语言里函数rand()和srand()的用法:

rand(void)用于产生一个伪随机unsigned int 整数。
srand(seed)用于给rand()函数设定种子。

srand()和rand()应该组合使用。一般来说,srand()是对rand()进行设置。
比如:
srand((UINT)GetCurrentTime());
int x = rand() % 100;
是生成 0 到 100 之间的随机数。

srand()是用来初始化随机种子数的,因为rand的内部实现是用线性同余法做的,他不是真的随机数,只不过是因为其周期特别长,所以有一定的范围里可看成是随机的,式子如下:

rand = rand*const_1 + c_var;
srand()函数就是给它的第一个rand值。

用"int x = rand() % 100;"来生成 0 到 100 之间的随机数这种方法是不或取的,
比较好的做法是: j=(int)(n*rand()/(RAND_MAX+1.0))   产生一个0到n之间的随机数

5.>总结
1)srand()给rand()提供种子
2)srand()中的seed一般由时间函数得,eg srand((UINT)GetCurrentTime()) srand( (unsigned)time( NULL ) ) time()函数得到现在的系统时间...等等

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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