
如果我们在程序中生成随机数,有必要控制数字的序列。
randomize()和srand()函数用于种子随机数生成器。
分配随机数生成器起始数字的过程称为种子生成器。
randomize()使用PC的时钟产生一个随机种子。
srand()允许我们指定随机数生成器的起始值。
程序
下面是C语言中关于rand的程序:
演示
#include<stdio.h>
int main(){
// create same sequence of
// random numbers on every time the program runs
for(int i = 0; i<h2>输出</h2>
<p>您将看到以下输出 −</p>
<pre class="brush:php;toolbar:false;">1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421
以下是关于srand的C程序:
在线演示
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(){
// create different sequence of
// random numbers on every time the program runs
// It Use current time as seed for random generator
srand(time(0));
for(int i = 0; i<h2>输出</h2>
<p>您将看到以下输出 −</p>
<pre class="brush:php;toolbar:false;">1919778910
1203408690
1755813469
1976428341
37040990
1849384103
986990763
2040061815
391541163
1718314135
13万字C语言保姆级教程(深入):立即使用
在学习笔记中,你将探索c语言的核心概念和高级技巧!











