随机数的问题

henghengpig 2004-04-14 08:33:33
用随机数产生5个大于0小于10的随机数存放于数组a[5]中,也可以使用链式结构,且防止这五个数字不可重复。
请给出算法,谢谢!
...全文
50 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackyRao 2004-04-14
  • 打赏
  • 举报
回复
本来这里的随机数都是伪随机数嘛
bshaozi 2004-04-14
  • 打赏
  • 举报
回复
(想飞的感觉)你的也有问题
少了一个{}
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( )
{
int i=0,j;
int temp;
int c[5];
int flag=0;
srand((unsigned)time( NULL ) );
while(i<5)
{
temp = rand()%9+1;
j=0;
while(j<i)
{
if(c[j] == temp)
//continue;
{ flag=1;
break;}
j++;
}
if(flag==1)
{
flag=0;
continue;
}
c[i] = temp;
printf("%d ", c[i]);
i++;
}
}
demo001 2004-04-14
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

void main()
{
srand((unsigned)time( NULL ) );
int num[5]={0};
int nMark[10]={0};
int j;
for(int i=0;i<10;i++)
nMark[i]=i;
for(i=0; i<5;i++)
{
j=rand()%(9-i)+1;
num[i]=nMark[j];
nMark[j]=nMark[9-i];
cout<<num[i]<<",";
}
}
zhangfjj 2004-04-14
  • 打赏
  • 举报
回复
我调试过呀,没得问题!
cycxp4363 2004-04-14
  • 打赏
  • 举报
回复
我这个和vzhangfjj(小张)的差不多,不过我认为,她那个有点问题所以:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( )
{
int i=0,j;
int temp;
int c[5];
int flag=0;
srand((unsigned)time( NULL ) );
while(i<5)
{
temp = rand()%9+1;
j=0;
while(j<i)
{
if(c[j] == temp)
//continue;
flag=1;
break;
j++;
}
if(flag==1)
{
flag=0;
continue;
}
c[i] = temp;
printf("%d ", c[i]);
i++;
}
}
henghengpig 2004-04-14
  • 打赏
  • 举报
回复
zhangfjj(小张) :你的算法调试过么?
demo001 2004-04-14
  • 打赏
  • 举报
回复
不过似乎不行

这点好像跟VB不一样:(
demo001 2004-04-14
  • 打赏
  • 举报
回复
srand( (unsigned)time( NULL ) )是使用系统时间作为随机种子

可以使得随机数不重复
Lemon_2000 2004-04-14
  • 打赏
  • 举报
回复

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main()
{
int num[5] = {0};
int nMark[10] = {0};
int i = 0;
while (1)
{
srand( (unsigned)time( NULL ) * 97 );
int n = rand() % 10;
if ( nMark[n] == 0)
{
nMark[n] = 1;
num[i] = n;
i++;
}
if (i == 5)
break;
}

for (i = 0; i < 5; i++)
printf("%4d", num[i]);

printf("\n");

}
zhangfjj 2004-04-14
  • 打赏
  • 举报
回复
不用goto就要用continue
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( )
{
int i=0,j;
int temp;
int c[5];
srand((unsigned)time( NULL ) );
while(i<5)
{
temp = rand()%9+1;
j=0;
while(j<i)
{
if(c[j] == temp)
continue;
j++;
}
c[i] = temp;
printf("%d ", c[i]);
i++;
}
}
积木 2004-04-14
  • 打赏
  • 举报
回复
给你一个思路使用一个数组
a[10] = {1,2,3,4,5,6,7,8,9,10}
每次产生一个1,10随机数,然后按照这个随机数的值i将a[i]取出
后面的向前移动,然后在产生一个1-9的随机数再取,再移动
这样产生的随机数就不会重复了,当然这个试用于你这样的小规模问题上
junnyfeng 2004-04-14
  • 打赏
  • 举报
回复



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>



int main(void)
{

int a[5];
int j,k=0,tm;
memset(a,-1,5*sizeof(int));
srand( (unsigned)time( NULL ) );

while(1)
{
tm=rand()%9+1;
for(j=0;j<5 && a[j]!=tm;j++);

if(a[j]==tm)
continue;
else
{
a[k++]=tm;
printf("%d\t",a[k-1]);
}

if(k==5)
break;
}

}
henghengpig 2004-04-14
  • 打赏
  • 举报
回复
如果不使用goto语句又该如何实现呢?
Januarius_ 2004-04-14
  • 打赏
  • 举报
回复
要求不重复的

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main()
{
int i;
int num[5] = {0};
srand( (unsigned)time( NULL ) );
for( i = 0; i < 5;i++ ) {
num[i] = rand() % 10;
tag: for(int j=0;j<i;j++) {
if(num[i] == num[j]) {
num[i] = rand() % 10;
goto tag;
}
}

}
}
freefalcon 2004-04-14
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
int i;

srand( (unsigned)time( NULL ) );

int c[5];
for( i = 0; i < 5; i++ )
{
int temp;
loop:
temp = rand()%9+1;
for(int j=0; j<i; j++)
{
if(c[j] == temp)goto loop;
}

c[i] = temp;

printf("%d ", c[i]);
}
}

难得有机会用goto,这里用一下感觉很合适
henghengpig 2004-04-14
  • 打赏
  • 举报
回复
srand( (unsigned)time( NULL ) );
可以使得这五个随机数不重复么?
Quain 2004-04-14
  • 打赏
  • 举报
回复
int num[5] = {0}后面少打了个分号,呵呵
Quain 2004-04-14
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main()
{
int i;
int num[5] = {0}
srand( (unsigned)time( NULL ) );
for( i = 0; i < 5;i++ )
num[i] = rand() % 10;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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