rand()函数可以产生[0,RAND_MAX]之间的均匀的伪随机数,它定义在头文件stdlib.h中,函数原型:

int rand(void);

C标准库的实现是:

unsigned long int next = ;

/*rand: return pseudo-number integer on 0...32767*/
int rand(void)
{
next = next* + ;
return (unsigned int)(next/) % ;
} /*srand: set seed for rand()*/
void srand(unsigned int seed)
{
next = seed;
}

如果没有初始化“随机数种子”,那么默认初始种子是1,1*1103515245+12345,return得到第一个伪随机数,接着将这个结果作为下次的种子,带入式子得到第二个伪随机数……

之所以定义为unsigned int,是防止数值溢出后不会出现负值。

直接调用rand(),会导致产生的是同一套随机数,所以我们使用srand()来初始化随机数种子。

要注意的是:不同编译器计算随机数的方法不尽相同,所以即使给srand()传递相同的参数,也可能产生不同的随机数序列。

举个栗子:

/*产生0-9的随机数*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h> int main(int argc,char** argv)
{
srand(time(NULL)); //初始化随机数种子
for(int i = ;i < ;i++)
{
printf("%d ",rand()%);
} return ;
}

利用rand()%n产生[0,n)之间的随机数,那么一旦n > RAND_MAX,这种做法就会失效。

如果你对精度的要求不高,可以采用如下办法:
先用rand()/RAND_MAX,得到[0,1]之间的随机实数,然后扩大n-1倍,四舍五入,就可得到[0,n-1]之间的随机数。

/*产生10个[0,99999]之间的随机数*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h> int main(int argc,char** argv)
{
int n = ;
double random_doub;
int random_num; srand(time(NULL)); //初始化随机数种子 for(int i = ;i < ;i++)
{
random_doub = (double)rand() / RAND_MAX; //生成[0,1]之间的随机数
random_num = (int)((n - )*random_doub + 0.5); //生成[0,n-1]之间的随机数
printf("%d ",random_num);
} return ;
}

Random Number Generator的更多相关文章

  1. 文献翻译|Design of True Random Number Generator Based on Multi-stage Feedback Ring Oscillator(基于多级反馈环形振荡器的真随机数发生器设计)

    基于多级反馈环形振荡器的真随机数发生器设计 摘要 真随机数生成器(trng)在加密系统中起着重要的作用.本文提出了一种在现场可编程门阵列(FPGA)上生成真随机数的新方法,该方法以 多级反馈环形振荡器 ...

  2. 【Codechef】Random Number Generator(多项式除法)

    题解 前置技能 1.多项式求逆 求\(f(x)\*g(x) \equiv 1 \pmod {x^{t}}\) 我们在t == 1时,有\(f[0] = frac{1}{g[0]}\) 之后呢,我们倍增 ...

  3. @codechef - RNG@ Random Number Generator

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  4. [spojRNG]Random Number Generator

    先将所有数加上Ri,即变为区间[0,2Ri],考虑容斥,将区间容斥为[0,+oo)-[2Ri,+oo),然后对[2Ri,+oo)令$bi=ai-2Ri$,相当于范围都是[0,+oo)问题转化为求n个正 ...

  5. Random number

    Original #include <stdlib.h> #include <time.h> srand(time(NULL)); rand(); The versions o ...

  6. Case Study: Random Number Generation(翻译教材)

    很荣幸,经过三天的努力.终于把自己翻译的教材做完了,现在把它贴出来,希望能指出其中的不足.   Case Study: Random Number Generation Fig. 6.7  C++ 标 ...

  7. ISO C Random Number Functions

    This section describes the random number functions that are part of the ISO C standard. To use these ...

  8. How to generate a random number in R

    Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif ...

  9. Linux shell get random number

    the Shell Profile: When a new interactive shell is started, /etc/profile, followed by /etc/bash.bash ...

随机推荐

  1. mysql 聚集函数 count 使用详解

    mysql 聚集函数 count 使用详解 本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName) 2.distinct 与 coun ...

  2. Thinkphp6源码分析之解析,Thinkphp6路由,Thinkphp6路由源码解析,Thinkphp6请求流程解析,Thinkphp6源码

    Thinkphp6源码解析之分析 路由篇-请求流程 0x00 前言: 第一次写这么长的博客,所以可能排版啊,分析啊,什么的可能会比较乱.但是我大致的流程已经觉得是说的够清楚了.几乎是每行源码上都有注释 ...

  3. Vue-CLI 3.x 部署项目至生产服务器

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. 本教程主要讲解的是 Vue ...

  4. CH5E07 划分大理石(背包dp+二进制拆分)

    传送门     大意: 有价值分别为1..6的大理石各a[1..6]块,现要将它们分成两部分,使得两部分价值之和相等,问是否可以实现.其中大理石的总数不超过20000. 解题思路: 妥妥的多重背包+二 ...

  5. Property [*****] not found on type [com.erp.pojo.ErpSupplier]

    我实体类里用的是 springboot 里@Slf4j   @Data  注解式写的  这样可以减少代码量 ,但是遇到一个问题影响我好长时间 出现了这个错误  Property [*****] not ...

  6. 06-移动web之flex布局

    一.基本概念 flex布局又叫伸缩布局 .弹性布局 .伸缩盒布局 .弹性盒布局 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. ...

  7. for循环,for…in循环,forEach循环的区别

    for循环,for…in循环,forEach循环的区别for循环通关for循环,生成所有的索引下标for(var i = 0 ; i <= arr.length-1 ; i++){ 程序内容 } ...

  8. Linux终端命令格式

    01.终端命令格式 command [-options] [parameter] 说明: command:命令名,响应功能的英文单词或单词的缩写 [-options]:选项,可用来对命令进行控制,也可 ...

  9. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  10. D. AB-string

    https://codeforces.com/contest/1238/problem/D 题目大意:统计good string的个数,good string的定义,给定的字符串中含有回文段落, 题解 ...