C 库函数 - srand()

C 语言教程 · 2019-02-24 11:12:56

描述

C 库函数 void srand(unsigned int seed) 播种由函数 rand 使用的随机数发生器。

声明

下面是 srand() 函数的声明。

void srand(unsigned int seed)

参数

  • seed -- 这是一个整型值,用于伪随机数生成算法播种。

返回值

该函数不返回任何值。

实例

下面的实例演示了 srand() 函数的用法。

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

int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   /* 初始化随机数发生器 */
   srand((unsigned) time(&t));

   /* 输出 0 到 50 之间的 5 个随机数 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }
   
  return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

38
45
29
29
47

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Cypherpunks

Cypherpunks

Julian Assange、Jacob Appelbaum、Andy Müller-Maguhn、Jérémie Zimmermann / OR Books / 2012-11 / GBP 8.99

Cypherpunks are activists who advocate the widespread use of strong cryptography (writing in code) as a route to progressive change. Julian Assange, the editor-in-chief of and visionary behind WikiLea......一起来看看 《Cypherpunks》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器