找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1231|回复: 11

srand函数是什么意思啊?

[复制链接]
发表于 2009-11-4 02:14:46 | 显示全部楼层 |阅读模式 IP:江苏扬州
我从网上找了一个产生随机数的程序
#include <iostream> #include <cstdlib> using namespace std;
int main() { int counter; for(counter=0;counter<10;counter++) { srand(counter+1); cout<<"Random number"<<counter+1<<":"<<rand()<<endl; } system("pause"); return 0; }
其中有
srand(counter+1);
srand是什么意思啊,起什么作用啊
还有
system("pause");
好象没用啊,我去掉了还照样运行
发表于 2009-11-4 02:14:46 | 显示全部楼层 IP:江苏扬州
srand(counter+1); 函数的作用是让你的随机函数rand()有种子数可依据,从而产生比较分散的伪随机数.
system("pause");是针对BCB,C++ Builder,Dev C++,GCC3.3 等编译器而用的,因为在它们当中编译控制台程序时必须要暂停一下来看结果,不然程序的控制台界面就会一闪就什么也没有了.也可以使用getchat();
回复

使用道具 举报

发表于 2009-11-4 02:14:47 | 显示全部楼层 IP:江苏扬州
怎么每次产生的随机数都一样的??能不能每次产生的都不一样??请指教!!
回复

使用道具 举报

发表于 2009-11-4 02:14:48 | 显示全部楼层 IP:江苏扬州
guanyou ,
这个程序在上面程序的基础上作了改动,这样每次执行后产生的结果就不一样了。
如果你有兴趣可以写一个 lotto 程序, lotto 是一种搏彩,玩法是这样的。在一个固定的数子范围内,猜数字,举例来讲,1 到 49 这 49 个数字中猜 6 个 数,也就是选出其中 6 个数, 比如,如果我选 2 4 6 8 10 12 而那天开奖的数和我的数一样,那我就中大奖了。大家应该注意到了,所选出的数没有一个会是重复的,因为选掉的数就不可以在选了呀,好了,大家如果有兴趣,就写写这个程序吧。国內各种福利彩票也很多,你的程序或许还能帮你中奖呢。
#include <iostream> #include <cstdlib> #include <ctime>
using namespace std;
int main() { int counter; srand( (unsigned)time( NULL ) );
for(int i = 0; i<10; i++) { cout<<"6 Random number(between 1 and 49): "; for(counter=0;counter<6;counter++) { cout<<rand()%49+1<<" "; } cout<<endl; } system("pause"); return 0; }
回复

使用道具 举报

发表于 2009-11-4 02:14:49 | 显示全部楼层 IP:江苏扬州
请问kai版主,
srand( (unsigned)time( NULL ) );中(unsigned)time( NULL )是不是根据时间产生随机数啊,unsigned和null分别代表什么意思啊
rand()%49+1是什么意思 ,是不是rand()产生的随机数除以49取余然后再加1,直接用rand()代替不行吗
回复

使用道具 举报

发表于 2009-11-4 02:14:50 | 显示全部楼层 IP:江苏扬州
#include <iostream> #include <cstdlib> // for srand() und rand() using namespace std;
int main() { unsigned int i, num;
cout<<"\nPlease enter a number between 1 and 65535: "; cin>>num;
srand(num); // initialize the the random-number generator printf("\n\n******* random-number *******\n"); for( i = 1 ; i <= 15 ; ++i) cout<<endl<<i<<".Random-number = "<<rand() % 200 + 1; cout<<endl; return 0; }
运行完上面这段程序,可以发现,如果你每次输入的数值是一样的,那么每次产生的随机数也是一样的,也就是说,以一个固定的数值作为种子是一个缺点。 通常的做法是 以这样一句代码srand((unsigned) time(NULL));来取代,这样将使得种子为一个不固定的数, 这样产生的随机数就不会每次执行都一样了。
回复

使用道具 举报

发表于 2009-11-4 02:14:52 | 显示全部楼层 IP:江苏扬州
载录于msdn
time
Gets the system time.
time_t time( time_t *timer );
RoutineRequired HeaderCompatibility
time<time.h>ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport library for MSVCRT.DLL, retail version

Return Value
time returns the time in elapsed seconds. There is no error return.
Parameter
timer
Storage location for time
Remarks
The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.
Example
/* TIMES.C illustrates various time and date functions including: *      time            _ftime          ctime       asctime *      localtime       gmtime          mktime      _tzset *      _strtime        _strdate        strftime * * Also the global variable: *      _tzname */#include <time.h>#include <stdio.h>#include <sys/types.h>#include <sys/timeb.h>#include <string.h>void main(){    char tmpbuf[128], ampm[] = "AM";    time_t ltime;    struct _timeb tstruct;    struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };    /* Set time zone from TZ environment variable. If TZ is not set,     * the operating system is queried to obtain the default value      * for the variable.      */    _tzset();    /* Display operating system-style date and time. */    _strtime( tmpbuf );    printf( "OS time:\t\t\t\t%s\n", tmpbuf );    _strdate( tmpbuf );    printf( "OS date:\t\t\t\t%s\n", tmpbuf );    /* Get UNIX-style time and display as number and string. */    time( &ltime );    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );    printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );    /* Display UTC. */    gmt = gmtime( &ltime );    printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );    /* Convert to time structure and adjust for PM if necessary. */    today = localtime( &ltime );    if( today->tm_hour > 12 )    {   strcpy( ampm, "PM" );   today->tm_hour -= 12;    }    if( today->tm_hour == 0 )  /* Adjust if midnight hour. */   today->tm_hour = 12;    /* Note how pointer addition is used to skip the first 11      * characters and printf is used to trim off terminating      * characters.     */    printf( "12-hour time:\t\t\t\t%.8s %s\n",       asctime( today ) + 11, ampm );    /* Print additional time information. */    _ftime( &tstruct );    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );    printf( "Zone difference in seconds from UTC:\t%u\n",              tstruct.timezone );    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );    printf( "Daylight savings:\t\t\t%s\n",              tstruct.dstflag ? "YES" : "NO" );    /* Make time for noon on Christmas, 1993. */    if( mktime( &xmas ) != (time_t)-1 )   printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );    /* Use time structure to build a customized time string. */    today = localtime( &ltime );    /* Use strftime to build a customized time string. */    strftime( tmpbuf, 128,         "Today is %A, day %d of %B in the year %Y.\n", today );    printf( tmpbuf );}
回复

使用道具 举报

发表于 2009-11-4 02:14:53 | 显示全部楼层 IP:江苏扬州
好学,
其实不必指名道姓的问,你有问题,直接问就可以了,我相信大家都会回答你的。
回复

使用道具 举报

发表于 2009-11-4 02:14:55 | 显示全部楼层 IP:江苏扬州
KAI先生写的的确达到了随机数,不过有一个小问题就是srand( (unsigned)time( NULL ) );之后
虽然每次出现的都不一样了,不过还是有一排里2到3个数相等的可能,因为rand()%49+1虽然是随机产生数不过不能保证每次产生的都不一样.
下面是我写的一段程序,因为没有这个问题:)
#include "stdafx.h" #include <algorithm> #include <iostream> #include <algorithm> #include <functional> #include <vector> #include <TIME.H> using namespace std; #define VECTOR_SIZE 49
int main(int argc, char* argv[]) { srand( (unsigned)time( NULL ) );
typedef vector<int> IntVector; typedef IntVector::iterator IntVectorIt; IntVector Numbers(VECTOR_SIZE) ; IntVectorIt sss, end, it; for(int i = 0,j = 0;i < VECTOR_SIZE;i++,j++) { Numbers[i] = j; } sss = Numbers.begin(); end = Numbers.end(); random_shuffle(sss, end); it = sss; for(int t = 0;t < 7;t++) { cout<<*it<<" "; it++; } system("pause"); return 0; }
经VC6.0++编译通过.
回复

使用道具 举报

发表于 2009-11-4 02:14:57 | 显示全部楼层 IP:江苏扬州
onicuka,我编译时出了问题,系统提示#include "stdafx.h"头文件找不到,为什么啊??我把它去掉了就能通过编译???
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 11:25 , Processed in 0.163841 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表