`

srand and rand

 
阅读更多

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;
}
luowenlong@im-vm07:~/code$ ./srand.out
First number: 83
First number: 86
First number: 77
First number: 15
First number: 93
First number: 35
First number: 86
First number: 92
First number: 49
First number: 21
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$ ./srand.out
First number: 83
First number: 86
First number: 77
First number: 15
First number: 93
First number: 35
First number: 86
First number: 92
First number: 49
First number: 21
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$ ./srand.out
First number: 83
First number: 86
First number: 77
First number: 15
First number: 93
First number: 35
First number: 86
First number: 92
First number: 49
First number: 21
luowenlong@im-vm07:~/code$ vi srand.cpp
/* 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;
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"srand.cpp" 18L, 358C written
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$ g++ -o srand.out srand.cpp
luowenlong@im-vm07:~/code$ ./srand.out
First number: 54
First number: 95
First number: 65
First number: 69
First number: 0
First number: 30
First number: 83
First number: 32
First number: 28
First number: 88
luowenlong@im-vm07:~/code$
luowenlong@im-vm07:~/code$ ./srand.out
First number: 4
First number: 85
First number: 33
First number: 77
First number: 3
First number: 66
First number: 72
First number: 86
First number: 12
First number: 26
luowenlong@im-vm07:~/code$ ./srand.out
First number: 28
First number: 39
First number: 96
First number: 97
First number: 99
First number: 74
First number: 64
First number: 34
First number: 94
First number: 76
luowenlong@im-vm07:~/code$
分享到:
评论

相关推荐

    VC随机函数srand和rand用法

    前言:编程中经常需要用到随机数,当然程序(函数)本身无法生成所谓的真实的...VC中随机函数最常用就是srand和rand(实际上是属于标准C函数),其中srand负责设置随机种子,rand则负责生成随机数。使用此二随机函数需

    有关C++中随机函数rand() 和srand() 的用法详解

    一、rand() 函数名: rand  功 能: 随机数发生器 用 法: int rand(void);  所在头文件: stdlib.h 函数说明 : rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在一定 的范围里可...

    用C语言的rand()和srand()产生伪随机数的方法总结

     从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x7fff))间的随机整数。  函数二:void srand(unsigned seed);  参数seed是rand()的种子,用来初始化rand()的起始值。  可以认为rand()在...

    C++中随机函数 rand() 和 srand() 的用法

    一、rand()  函数名: rand  功 能: 随机数发生器  用 法: int rand(void);  所在头文件: stdlib.h  函数说明 :  · rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在...

    生成随机数rand函数的用法详解

    函数rand()是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。而使用同种子相同的数调用 srand()会导致相同的随机数...

    详解C语言中rand函数的使用

    前言 我们在编程实现算法的过程中,往往需要使用到随机数。由于计算机是一台以逻辑为基础的机器,没法做到真正的随机(大概量子...3、srand( (unsigned)time( NULL ) ); /*选取种子文件*/ 4、for( i = 0; i &lt;20;i

    C++ 中随机函数random函数的使用方法

    C++ 中随机函数random函数的使用方法 一、random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过。... 2、C++中另一函数srand(),可以指定不同的数(无符号整数变元)为种子。但是如果种子相同,伪随机数列也相

    猜数游戏 c++源代码

    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沙箱

    Fortran 此代表包含用FORTRAN编写的基本程序。... system_clock srand rand svp.in Collat​​z序列 最大整数 kind huge Collat​​z延迟记录 捕捉中断信号 common signal state 极值 huge tiny 新

    C标准库函数.CHM

    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 ...

    random:随机提供现代C ++和便捷的API

    auto random_number = (rand() % ( 9 - 1 )) + 1 ; // get a pseudo-random integer between 1 and 9 问题 应该指定种子 应该写自己的发行版algorihtm 无法保证所产生的随机序列的质量。 C ++ 11风格 std::...

    php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍

    注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已被自动完成。 语法 shuffle(array) 参数 描述 array 必需。规定要使用的数组。 例子 复制代码 代码如下: &lt;?php $my_...

    折线图图表(Chart)效果

    x -axis and y-axis can zoom in and out , you can scroll , double-click can restore the original size.使用方法:1、将“ARLineChart”文件夹内所有文件加入到工程中即可。 2、在需要的地方引入头文件: #...

    游戏.sln一个游戏的代码类鞥看懂的就看看吧

    , 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得随机数求均值、方差、正态分布的方法

    本文实例讲述了C#利用Random得随机数求均值、方差、正态分布的方法。分享给大家供大家参考。具体如下: 最近在做中小学试卷分析系统,其中...首先利用“srand((unsigned)time(NULL))”语句设置产生随机数的开始点(需要

    软件课程设计 试验报告 代码 演示

    2) 出题函数,也是本程序最关键的一个函数,通过使用“rand()%10”或“rand()%100”来获得一个0到9的一位整数随机值或得到0到99的两位整数随机值来为用户出题,并判断用户答案的对错; 3) 评分系统,是在用户...

    C++-写一个聊天机器人.pdf

    这个机器⼈也⽤下⾯的语⾔完成了:Java, Visual Basic, C#, Pascal, Prolog and Lisp(地址就在原帖⾥) 就像上⾯的程序⼀样,写⼀个能和⼈交流的⼩程序并不需要多少⾏代码,但是写⼀个能够读懂⼈说什么并恰当的回复...

    可以仿造ip

    #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 #...

    Perl Security pdf

    Platform and version details........................................................................................................ 3 The course notes....................................................

    C语言程序设计实例(200行)

    /*system time and date*/ printf(ctime(&lt;)); /*english format output*/ printf(asctime(localtime(&lt;)));/*tranfer to tm*/ printf(asctime(gmtime(&lt;))); /*tranfer to Greenwich time*/ } 【程序92】 题目:...

Global site tag (gtag.js) - Google Analytics