#include <stdlibh>
#include <stdioh>
#include <timeh> /用到了time函数,所以要有这个头文件/
#define MAX 10
int main( void)
{
int number[25] = {0};
int i;
printf("请输入25个任意整数:\n");
for(i=0;i<MAX;i++)
scanf("%d",&number[i]);
printf("输入的25个任意整数为:\n");
for(i=0;i<MAX;i++)
printf("%d ",number[i]);
printf("\n");
srand((unsigned) time(NULL)); /播种子/
printf("随机取的一个数为:%d",number[rand() % MAX]);
printf("\n");
return 0;
} 手动输入25个整数,程序自动随机输出25个数之内的一个数。 你不想手动输入也可以,那就先对数组进行赋值,源程序中把数组赋值那部分去掉就OK了!
'''''如果你是随机选50个不重复的数则可用下面这个程序:
Option Base 1
Private Sub Form_Click()
Dim A(), i%, k%, Jk%
ReDim A(50)
For i = 1 To 50
A(i) = i
Next
Choose:
k = UBound(A) ''''''K为当前还剩下未被抽取的数
Jk = Int(Rnd (k) + 1) ''''在剩余数中随机抽取第JK个数
Print A(Jk) ''''''''''''''''A(JK)就是随机抽到的数,
If k = 1 Then Exit Sub '''''如果是最后一个数则退出
For j = Jk To k - 1
A(j) = A(j + 1) ''''''从抽到的那个数开始,让前一个数等于后一个数,将抽到的那个数删除
Next j
ReDim Preserve A(k - 1)
GoTo Choose '''''继续随机抽选
End Sub
#include<stdioh>
#include <conioh>
#include <stdlibh>
#include <memoryh>
#include <stringh>
#include <timeh>
#define AWARDER_NUM 10
typedef struct tag_award
{
char ID[20];
char name[20];
char telNo[20];
char address[50];
}AWARDER;
AWARDER awarder = new AWARDER[AWARDER_NUM];
int first_award[1] = {-1};
int second_award[2] = {-1,-1};
int third_award[5] ={-1,-1,-1,-1,-1};
int awarder_num = 0;
void info_login()
{
if(awarder_num >= AWARDER_NUM)
{
AWARDER new_awarder = new AWARDER[awarder_num + AWARDER_NUM];
memcpy(new_awarder,awarder,awarder_numsizeof(AWARDER));
delete awarder;
awarder = new_awarder;
}
printf("身份z号:");
scanf("%s",awarder[awarder_num]ID);
printf("姓名:");
scanf("%s",awarder[awarder_num]name);
printf("电话号码:");
scanf("%s",awarder[awarder_num]telNo);
printf("家庭住址:");
scanf("%s",awarder[awarder_num]address);
awarder_num++;
}
void info_delete()
{
char name[20];
printf("输入您要删除的抽奖者的姓名:");
scanf("%s",name);
for(int i=0;i<awarder_num;i++)
{
if (strcmp(awarder[i]name,name) == 0)
{
printf("您选择删除的抽奖者的信息如下:\n");
printf("身份z号:%s\n",awarder[i]ID);
printf("姓名:%s\n",awarder[i]name);
printf("电话号码:%s\n",awarder[i]telNo);
printf("家庭住址:%s\n",awarder[i]address);
break;
}
}
if(i == awarder_num)
{
printf("没有找到您输入的抽奖者!\n");
}
else
{
char c;
printf("您确定要删除吗?(Y//N)");
fflush(stdin);
scanf("%c",&c);
if(c == 'Y' || c == 'y')
{
AWARDER new_award = new AWARDER[awarder_num-1];
if(i <= 0)
memcpy(new_award,awarder+1,sizeof(AWARDER) (awarder_num - 1));
else if( i >= awarder_num -1)
memcpy(new_award,awarder,sizeof(AWARDER) (awarder_num - 1));
else
{
memcpy(new_award,awarder,sizeof(AWARDER) i);
memcpy(new_award+i,awarder+i+1,sizeof(AWARDER) (awarder_num - i - 1));
}
awarder_num--;
delete awarder;
awarder = new_award;
}
}
}
void info_modify()
{
char name[20];
printf("输入您要修改的抽奖者的姓名:");
scanf("%s",name);
for(int i=0;i<awarder_num;i++)
{
if (strcmp(awarder[i]name,name) == 0)
{
printf("您选择的抽奖者的信息如下:\n");
printf("身份z号:%s\n",awarder[i]ID);
printf("姓名:%s\n",awarder[i]name);
printf("电话号码:%s\n",awarder[i]telNo);
printf("家庭住址:%s\n",awarder[i]address);
break;
}
}
if(i == awarder_num)
{
printf("没有找到您输入的抽奖者!\n");
}
else
{
printf("请输入该抽奖者的新信息:\n");
printf("身份z号:");
scanf("%s",awarder[i]ID);
printf("姓名:");
scanf("%s",awarder[i]name);
printf("电话号码:");
scanf("%s",awarder[i]telNo);
printf("家庭住址:");
scanf("%s",awarder[i]address);
}
}
bool check_over(int cur_award,int awarderNo)
{
if(awarderNo == 1)
{
if(cur_award == first_award[0])
return true;
}
else if(awarderNo <= 3)
{
if(cur_award == first_award[0])
return true;
for(int i=0;i<awarderNo-1;i++)
if(cur_award == second_award[i])
return true;
}
else if(awarderNo <= 8)
{
if(cur_award == first_award[0])
return true;
for(int i=0;i<2;i++)
if(cur_award == second_award[i])
return true;
for(i=0;i<awarderNo-3;i++)
if(cur_award == third_award[i])
return true;
}
return false;
}
void award()
{
if(awarder_num < 8)
{
printf("人数太少,请添加足够的人再抽奖!\n");
return;
}
srand((long)time(0));
char hit_id[20];
printf("\n开始抽第一等奖(1名),按任意键开始\n");
getch();
while(!kbhit())
{
first_award[0] = rand() % awarder_num;
memcpy(hit_id,awarder[first_award[0]]ID,18);
printf("\r ");
printf("\r %s",hit_id);
}
printf("\n\n开始抽第二等奖(2名)");
for (int i=0;i<2;i++)
{
printf("\n第%d个二等奖,按任意键开始\n",i+1);
getch();
while(!kbhit())
{
second_award[i] = rand() % awarder_num;
while(check_over(second_award[i],1+i))
second_award[i] = rand() % awarder_num;
memcpy(hit_id,awarder[second_award[i]]ID,18);
printf("\r ");
printf("\r %s",hit_id);
}
}
printf("\n\n开始抽第三等奖(5名)");
for (i=0;i<5;i++)
{
printf("\n第%d个三等奖,按任意键开始\n",i+1);
getch();
while(!kbhit())
{
third_award[i] = rand() % awarder_num;
while(check_over(third_award[i],3+i))
third_award[i] = rand() % awarder_num;
memcpy(hit_id,awarder[third_award[i]]ID,20);
printf("\r ");
printf("\r %s",hit_id);
}
}
}
void info_show()
{
printf("一等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[first_award[0]]ID);
printf("\t姓名:%s\n",awarder[first_award[0]]name);
printf("\t电话号码:%s\n",awarder[first_award[0]]telNo);
printf("\t家庭住址:%s\n",awarder[first_award[0]]address);
printf("二等奖获得者:\n");
for (int i=0;i<2;i++)
{
printf("第%d个二等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[second_award[i]]ID);
printf("\t姓名:%s\n",awarder[second_award[i]]name);
printf("\t电话号码:%s\n",awarder[second_award[i]]telNo);
printf("\t家庭住址:%s\n",awarder[second_award[i]]address);
}
printf("三等奖获得者:\n");
for (int i=0;i<5;i++)
{
printf("第%d个三等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[third_award[i]]ID);
printf("\t姓名:%s\n",awarder[third_award[i]]name);
printf("\t电话号码:%s\n",awarder[third_award[i]]telNo);
printf("\t家庭住址:%s\n",awarder[third_award[i]]address);
}
}
/提示信息/
bool raise()
{
printf("\n请输入你需要的 *** 作代号:\n");
printf("\t1录入:完成抽奖者信息的输入;\n");
printf("\t2删除:完成抽奖者信息的删除;\n");
printf("\t3修改:允许对已经录入的数据重新进行编辑、修改;\n");
printf("\t4抽奖:开始抽奖!\n");
printf("\t5显示:显示所有中奖者的信息;\n");
printf("\t6退出。\n");
printf("==>");
int operate;
fflush(stdin);
scanf("%d",&operate);
switch(operate)
{
case 1:
info_login();
break;
case 2:
info_delete();
break;
case 3:
info_modify();
break;
case 4:
award();
break;
case 5:
info_show();
break;
case 6:
return false;
default:
break;
}
printf("\n *** 作完成。\n");
return true;
}
void main()
{
while(raise());
}
include <stdioh>
include <randomh>
void main(void)
{
int i,j;
int rad;
int a[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
for(i = 0; i < 10; i++)
{
rad = random(10);
for(j = 0; j < 10; j++)
{
if(rad != a[j])
a[i] = rad;
}
}
for(i = 0; i < 10; i++)
{
printf("%d,\n",a[i]);
}
}
其实不必要写成子程序,看你需要多少位的随机数,要是8位的,就在主循环程序中放一条
INC NUMBER 指令,NUMBER是定义的某个内部RAM单元,如:NUMBER EQU 30H,需要随机数时,从NUMBER中读取就行了,随机读取的,这个数也是随机的了。要是需要16位的,就用两个内部RAM单元来随机计数,一个做低8位,一个做8位就行了。在主循环程序中放如下指令:
MOV A , NUMBERL
INC A
MOV NUBBERL, A ;保存低8位
JNZ TONEXT
INC NUMBERH ;高8位加1
TONEXT: 其它指令
同样,用随机数时就从这两个单元中读取就行了。
首先要确定这100个数的范围,比如0-99。
然后抽取随机数,并利用模除限定范围,每当抽取到一个新的数时,同历史数据比较,如果有重复则放弃。 直到抽取到不重复的10个数为止。
示例代码如下:
#include <stdioh>#include <stdlibh>
#include <timeh>
int main()
{
int a[10]; // 存储结果。
int i, j;
srand(time(NULL));//通过获取时间值来设定随机数种子,以保证每次运行结果不同。
for(i = 0; i < 10; )//注意,第三项留空,因为需要判断每次得到的数值是否重复,如重复则不进行i的自加
{
a[i] = rand()%100; //得到一个0-99中的随机数。
for(j = 0; j < i; j ++)
if(a[j] == a[i])break;//发现重复,结束内循环
if(j == i) i ++;//如果i和j相等,代表内循环是达到条件退出的,即不是通过break,也就是没有重复项。
}
for(i = 0; i < 10; i++) //输出得到的数值。
printf("%d,", a[i]);
printf("\n");
return 0;
}
以下是运行该程序三次分别得到的结果(每行一次),可以看到是实现了所有功能的。
96,84,85,30,29,97,92,51,38,81,49,42,13,18,24,1,75,91,60,5,
68,16,23,79,17,59,83,82,34,70,
以上就是关于c语言 求25个数取一个随机数的代码全部的内容,包括:c语言 求25个数取一个随机数的代码、vb抽取1~55随机数【班级抽签用】、C语言根据随机数编写一个抽奖程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)