luowenlong@im-vm07:~/code$ vi srand.cpp
/* srand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
for(int i = 0; i < 10; i++)
printf ("First number: %d\n", rand() % 100);
/* srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
printf ("Again the first number: %d\n", rand() %100);
*/
return 0;
}
/* srand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
srand ( time(NULL) );
for(int i = 0; i < 10; i++)
printf ("First number: %d\n", rand() % 100);
/* srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
printf ("Again the first number: %d\n", rand() %100);
*/
return 0;
}
分享到:
相关推荐
前言:编程中经常需要用到随机数,当然程序(函数)本身无法生成所谓的真实的...VC中随机函数最常用就是srand和rand(实际上是属于标准C函数),其中srand负责设置随机种子,rand则负责生成随机数。使用此二随机函数需
一、rand() 函数名: rand 功 能: 随机数发生器 用 法: int rand(void); 所在头文件: stdlib.h 函数说明 : rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在一定 的范围里可...
从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x7fff))间的随机整数。 函数二:void srand(unsigned seed); 参数seed是rand()的种子,用来初始化rand()的起始值。 可以认为rand()在...
一、rand() 函数名: rand 功 能: 随机数发生器 用 法: int rand(void); 所在头文件: stdlib.h 函数说明 : · rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在...
函数rand()是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。而使用同种子相同的数调用 srand()会导致相同的随机数...
前言 我们在编程实现算法的过程中,往往需要使用到随机数。由于计算机是一台以逻辑为基础的机器,没法做到真正的随机(大概量子...3、srand( (unsigned)time( NULL ) ); /*选取种子文件*/ 4、for( i = 0; i <20;i
C++ 中随机函数random函数的使用方法 一、random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过。... 2、C++中另一函数srand(),可以指定不同的数(无符号整数变元)为种子。但是如果种子相同,伪随机数列也相
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have ...computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout (2) ; 控制输出显示2位小数
Fortran 此代表包含用FORTRAN编写的基本程序。... system_clock srand rand svp.in Collatz序列 最大整数 kind huge Collatz延迟记录 捕捉中断信号 common signal state 极值 huge tiny 新
2.13.6.6 srand 2.13.7 Multibyte Functions 2.13.7.1 mblen 2.13.7.2 mbstowcs 2.13.7.3 mbtowc 2.13.7.4 wcstombs 2.13.7.5 wctomb 2.14 string.h 2.14.1 Variables and Definitions 2.14.2 memchr ...
auto random_number = (rand() % ( 9 - 1 )) + 1 ; // get a pseudo-random integer between 1 and 9 问题 应该指定种子 应该写自己的发行版algorihtm 无法保证所产生的随机序列的质量。 C ++ 11风格 std::...
注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已被自动完成。 语法 shuffle(array) 参数 描述 array 必需。规定要使用的数组。 例子 复制代码 代码如下: <?php $my_...
x -axis and y-axis can zoom in and out , you can scroll , double-click can restore the original size.使用方法:1、将“ARLineChart”文件夹内所有文件加入到工程中即可。 2、在需要的地方引入头文件: #...
, SRCAND); putimage(100, 200, &imgSrc;, SRCPAINT); _getch(); closegraph(); return 0; } #endif //时间及文本显示 #if 0 int main() { initgraph(640, 480); time_t start, end; time(&start;); ...
本文实例讲述了C#利用Random得随机数求均值、方差、正态分布的方法。分享给大家供大家参考。具体如下: 最近在做中小学试卷分析系统,其中...首先利用“srand((unsigned)time(NULL))”语句设置产生随机数的开始点(需要
2) 出题函数,也是本程序最关键的一个函数,通过使用“rand()%10”或“rand()%100”来获得一个0到9的一位整数随机值或得到0到99的两位整数随机值来为用户出题,并判断用户答案的对错; 3) 评分系统,是在用户...
这个机器⼈也⽤下⾯的语⾔完成了:Java, Visual Basic, C#, Pascal, Prolog and Lisp(地址就在原帖⾥) 就像上⾯的程序⼀样,写⼀个能和⼈交流的⼩程序并不需要多少⾏代码,但是写⼀个能够读懂⼈说什么并恰当的回复...
#define WIN32_LEAN_AND_MEAN #include #include #include #include #include #define OICQ_MAX_PACKET 1024 #define OICQ_MAX_MSG 512 #define OICQ_MSG_LEN 45 #...
Platform and version details........................................................................................................ 3 The course notes....................................................
/*system time and date*/ printf(ctime(<)); /*english format output*/ printf(asctime(localtime(<)));/*tranfer to tm*/ printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ } 【程序92】 题目:...