关于c语言制作模拟彩票生成器的问题

linxinbin1993 2012-08-03 10:58:10
初学编程不久,想完成书上一道制作制作彩票生成器的小程序。遇到如下问题:
要求生成一组5=6个号码的随机数字,范围1~33。我利用srand函数获取系统时间作为rand函数的种子,生成号码后,检查是否有重复,若有就要求重新生成。可我弄了好久,生成的号码一直还是相同的,问题是否出现在检查那里?
我之前是利用for循环逐个检查,然后里面用if判断
部分代码修改后如下:
//红 6 1~33 蓝 1 1~16 红色不重复,使用冒泡排序对红色进行排序

//检查完之后,逐个输出,并排序

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

void main(){
//声明红色球数组,及蓝色球变量
int red[33],blue;
//
//生成并检查红色球是否有重复出现
for(int i=0;i<6;i++){
do{
for(int j=0;j<33;j++){
srand(time(NULL));
red[j]=rand()%33+1;
}
//循环遍历检查
//此处为要写检查的代码


}
}while();
printf("%d\t",red[i]);

//生成蓝色球
srand(time(NULL));
blue = rand()%16+1;

printf("\n%d\n",blue);
}
...全文
717 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
linxinbin1993 2012-08-03
  • 打赏
  • 举报
回复
非常感谢,问题基本解决了[Quote=引用 8 楼 的回复:]
引用 7 楼 的回复:

谢谢。不过编译时,里面好像有错误引用 3 楼 的回复:
C/C++ code
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
using namespace std……
[/Quote]
Xomic 2012-08-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

谢谢。不过编译时,里面好像有错误引用 3 楼 的回复:
C/C++ code
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
using namespace std;
bool func1……
[/Quote]

你不要全部拷贝进你的编译器,你只引用那个func1函数跟main函数里面的内容!前面那头文件我是做别的事情时候加的....
linxinbin1993 2012-08-03
  • 打赏
  • 举报
回复
谢谢。不过编译时,里面好像有错误[Quote=引用 3 楼 的回复:]
C/C++ code
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
using namespace std;
bool func1(int *p)
{
for(int i = 0;……
[/Quote]
Xomic 2012-08-03
  • 打赏
  • 举报
回复
srand(time(NULL));放while前,我把这句给删了...抱歉!
linxinbin1993 2012-08-03
  • 打赏
  • 举报
回复
大侠,我的问题可能没你想的复杂,况且我刚学这个,你的代码里我一些还没学,看不懂呢。不过还是谢谢你[Quote=引用 1 楼 的回复:]
C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned long ulrand(void) {
return (
(((unsigned long)rand()<<24)&0xFF000000ul)
|(((unsigned long)rand()<<1……
[/Quote]
lx3275852 2012-08-03
  • 打赏
  • 举报
回复
srand,只用一次~进入main函数 用一次就够了,程序不退出就不要用第二次
Xomic 2012-08-03
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
using namespace std;
bool func1(int *p)
{
for(int i = 0;i<=8;i++)
for(int j = i+1;j<=8;j++)
{
if(p[i] == p[j])
return false;
else
continue;
}
return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
int ch[9];
while(!func1(ch))
{
for(int i= 0;i<=8;i++)
ch[i] = rand()%10;
}
for(int i= 0;i<=8;i++)
{
cout<<ch[i]<<" ";
}
cin.get();
return 0;
}
Xomic 2012-08-03
  • 打赏
  • 举报
回复
我昨天还写了个生成0-9的你看看:

bool func1(int *p)
{
for(int i = 0;i<=8;i++)
for(int j = i+1;j<=8;j++)
{
if(p[i] == p[j])
return false;
else
continue;
}
return true;
}

int _tmain(int argc, _TCHAR* argv[])
{

int ch[9];
while(!func1(ch))
{
for(int i= 0;i<=8;i++)
ch[i] = rand()%10;
}
for(int i= 0;i<=8;i++)
{
cout<<ch[i]<<" ";
}
return 0;
}
赵4老师 2012-08-03
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned long ulrand(void) {
return (
(((unsigned long)rand()<<24)&0xFF000000ul)
|(((unsigned long)rand()<<12)&0x00FFF000ul)
|(((unsigned long)rand() )&0x00000FFFul));
}
int i;
unsigned long ul;
void main() {
srand(time(NULL));
for (i=0;i<10;i++) {
ul=ulrand();
printf("%010lu 0x%08x\n",ul,ul);
}
}
//2337588057 0x8b54c359
//1977377429 0x75dc6295
//3607316769 0xd7034921
//0009828482 0x0095f882
//3350859779 0xc7ba1003
//0945794621 0x385fae3d
//3400869024 0xcab524a0
//3097846779 0xb8a563fb
//4287421124 0xff8cdac4
//3934016258 0xea7c5302

70,020

社区成员

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

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