| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 719 人关注过本帖
标题:[求助] 关于随机函数问题!!
只看楼主 加入收藏
gonua
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-4-16
收藏
 问题点数:0 回复次数:7 
[求助] 关于随机函数问题!!

这个题是要求产生随机数并把这些数存到2维数组里,第一个选项是控制随机数的产生,
是要求如果在进入这个选项之前程序是可以产生随机数的,就关掉,这时候数组显示连续的数,
如果在进入这个选项之前产生的是连续的数,就打开,产生随机数。数组是nxn。随机数从0~255.
我不会这个阿~~这个选项是没有数据输入的,直接就显示是关还是开。。。。我都快晕了。。。

结果显示为:

menu
1. Seeding
2. Fill array
3. Exit
please enter number: 1

Seeding
-------
Random seeding is turned off.

menu
1. Seeding
2. Fill array
3. Exit
please enter number: 2

Array
-----
enter the size of array: 4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

menu
1. Seeding
2. Fill array
3. Exit
please enter number: 1

Seeding
-------
Random seeding is turned on.

menu
1. Seeding
2. Fill array
3. Exit
please enter number: 2

Array
-----
enter the size of array: 4

23 12 253 34
5 36 115 65
10 18 92 244
53 32 9 8


我写的code:
#include <stdio.h>
#include <time.h>

#define ROWS 15
#define COLS 15

void buffer();
void Seeding(int array[][]);
void FillArray(int array[][]);

int main()
{
char choice;
int array[ROWS][COLS] = {0};
int done = 1;

while(done)
{
printf("\n Main Menu ");
printf("\n---------------------------");
printf("\n1) Seeding");
printf("\n2) Fill array");
printf("\n3) Exit");
printf("\n---------------------------");
printf("\nEnter your option [1-3] : ");

choice = getchar();
buffer();

switch(choice)
{
case '1':
Seeding(array);
break;
case '2':
FillArray(array);
break;
case '3':
exit(0);
break;
default :
printf("\nInvalid input! Please try again!\n");
}
}

return 0;
}

//这个不知道怎么写,请各位帮帮我~~~
void Seeding(int array[][])
{
int status;

printf("\nSeeding");
printf("\n--------------");

if(status == 1)
{
srand(time(NULL));
printf("\nRandom seeding is turned on.");
}
else if(status == 0)
{
srand(0);
printf("\n\nRandom seeding is turned off.");
}

}

void FillArray(int array[ROWS][COLS])//这个我只写了能产生随机数的。。。
{
int i, j;
int num;
int done = 1;

printf("\nFill array");
printf("\n----------");

printf("\n\nEnter the size of array : ");

while(done)
{
if(scanf("%d", &num) == 1)
{
buffer();
if(num > 1 && num < 16)
{
for(i = 0; i < num; i++)
{
for(j = 0; j < num; j++)
{
array[i][j] = rand()%255;
printf("%2d ", array[i][j]);
}
printf("\n");
}
return;
}
else
{
printf("\nInvalid input! Please try again!\n");
break;
}
}
else
{
printf("\nInvalid input! Please try again!\n");
buffer();
break;
}
}
}

void buffer()
{
int ch;

while ((ch = fgetc(stdin)) != '\n' && ch != EOF)
;
clearerr(stdin);
}

[此贴子已经被作者于2007-3-20 0:43:32编辑过]

搜索更多相关主题的帖子: 随机数 随机函数 Seeding seeding 选项 
2007-03-20 00:42
江南孤峰
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-8
收藏
得分:0 

//可以这样
void Seeding(int array[][])
{
static int status = 0; // 默认关闭

printf("\nSeeding");
printf("\n--------------");

if(status == 1)
{
srand(time(NULL));
printf("\nRandom seeding is turned on.");
}
else if(status == 0)
{
srand(0);
printf("\n\nRandom seeding is turned off.");
}

}

2007-03-20 08:01
gonua
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-4-16
收藏
得分:0 
我试了,不行啊~~~总是turned off。
2007-03-20 12:34
hujian100
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-14
收藏
得分:0 

在楼主的启发下我刚编了个程序,基本符合你的要求吧,我没有用数组,另外使用了全局变量on_off来传值。程序如下:

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

int Seeding(int);
int Fillarray(int);

int on_off=1;
/*on_off为奇数时可产生随机数,on_off为偶数时则不可。一开始系统默认可以产生随机数*/

int main()
{
int choice;
while(1)
{
printf("\n Main Menu ");
printf("\n---------------------------");
printf("\n1) Seeding");
printf("\n2) Fill array");
printf("\n3) Exit");
printf("\n---------------------------");
printf("\nPlease input your choices(1~3):");
scanf("%d",&choice);
switch(choice)
{
case 1 : Seeding(choice);
break;
case 2 : Fillarray(on_off);
break;
case 3 : exit(0);
break;
default: printf("Input error!Please try it again!\n");
break;
}
}
return 0;
}

int Seeding(choice)
{
on_off+=choice;
if(on_off%2!=0)
{
printf("\nRandom seeding is turned on.\n");
return on_off;
}
else
{
printf("\nRandom seeding is turned off.\n");
return on_off;
}
}

int Fillarray(on_off)
{
int row,col,num=1,arraysize;
printf("Enter the size of array:");
scanf("%d",&arraysize);
if(on_off%2==0)
{
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",num++);
printf("\n");
}
}
else
{
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",rand()%255);
printf("\n");
}
}
return 0;
}


2007-03-20 16:11
gonua
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-4-16
收藏
得分:0 

非常感谢~~ 不过我想问你一下,为什么你没用srand(time(NULL)) ?

[此贴子已经被作者于2007-3-20 19:16:38编辑过]

2007-03-20 19:12
hujian100
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-14
收藏
得分:0 

改了:

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

int Seeding(int);
int Fillarray(int);

int on_off=1;
/*on_off为奇数时可产生随机数,on_off为偶数时则不可。一开始系统默认可以产生随机数*/

int main()
{
int choice;
while(1)
{
printf("\n Main Menu ");
printf("\n---------------------------");
printf("\n1) Seeding");
printf("\n2) Fill array");
printf("\n3) Exit");
printf("\n---------------------------");
printf("\nPlease input your choices(1~3):");
scanf("%d",&choice);
switch(choice)
{
case 1 : Seeding(choice);
break;
case 2 : Fillarray(on_off);
break;
case 3 : exit(0);
break;
default: printf("Input error!Please try it again!\n");
break;
}
}
return 0;
}

int Seeding(choice)
{
on_off+=choice;
if(on_off%2!=0)
{
printf("\nRandom seeding is turned on.\n");
return on_off;
}
else
{
printf("\nRandom seeding is turned off.\n");
return on_off;
}
}

int Fillarray(on_off)
{
int row,col,num=1,arraysize;
printf("Enter the size of array:");
scanf("%d",&arraysize);
if(on_off%2==0)
{
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",num++);
printf("\n");
}
}
else
{
srand(time(NULL));
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",rand()%255);
printf("\n");
}
}
return 0;
}


2007-03-26 23:30
wuwei168668
Rank: 1
等 级:新手上路
帖 子:154
专家分:0
注 册:2007-3-11
收藏
得分:0 
看得好头晕啊。。
我的实力还不够看这些程序。。。

学C语言难得过老外学用中国的筷子吗?
2007-03-27 00:26
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 

修改了楼上兄弟的代码,别介意.呵呵.这样可以动态决定是产生随机数组,还是连续的数组.也方便阅读些.
#include <stdio.h>
#include <stdlib.h>

int Seeding(int);
int Fillarray(int);

int on_off=1;
/*on_off为奇数时可产生随机数,on_off为偶数时则不可。一开始系统默认可以产生随机数*/

int main()
{
int choice;
while(1)
{
printf("\n Main Menu ");
printf("\n---------------------------");
printf("\n1) Seeding close");
printf("\n2) Seeding open");
printf("\n3) Fill array");
printf("\n4) Exit");
printf("\n---------------------------");
printf("\nPlease input your choices(1~4):");
scanf("%d",&choice);
switch(choice)
{
case 1 : Seeding(choice);
break;
case 2 : Seeding(choice);
break;
case 3 : Fillarray(on_off);
break;
case 4 : exit(0);
break;
default: printf("Input error!Please try it again!\n");
break;
}
}
return 0;
}

int Seeding(choice)
{
on_off+=choice;
if(on_off%2!=0)
{
printf("\nRandom seeding is turned on.\n");
return on_off;
}
else
{
printf("\nRandom seeding is turned off.\n");
return on_off;
}
}

int Fillarray(on_off)
{
int row,col,num=1,arraysize;
printf("Enter the size of array:");
scanf("%d",&arraysize);
if(on_off%2==0)
{
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",num++);
printf("\n");
}
}
else
{
for(row=0;row<arraysize;row++)
{
for(col=0;col<arraysize;col++)
printf("%d\t",rand()%255);
printf("\n");
}
}
return 0;
}




学习需要安静。。海盗要重新来过。。
2007-03-29 16:34
快速回复:[求助] 关于随机函数问题!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029851 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved