Random number
Original
#include <stdlib.h>
#include <time.h>
srand(time(NULL));
rand();
The versions of rand() and srand() in the Linux C Library use the same random number generator as random(3) and srandom(3), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher-order bits. Do not use this function in applications intended to be portable when good randomness is needed. (Use random(3) instead.)
Better
#include <stdlib.h>
#include <time.h>
srandom(time(NULL));
random();
The random() function uses a nonlinear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to RAND_MAX. The period of this random number generator is very large, approximately 16 * ((2^31) - 1). The srandom() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by random(). These sequences are repeatable by calling srandom() with the same seed value. If no seed value is provided, the random() function is automatically seeded with a value of 1. The initstate() function allows a state array state to be initialized for use by random(). The size of the state array n is used by initstate() to decide how sophisticated a random number generator it should use—the larger the state array, the better the random numbers will be. seed is the seed for the initialization, which specifies a starting point for the random number sequence, and provides for restarting at the same point. The setstate() function changes the state array used by the random() function. The state array state is used for random number generation until the next call to initstate() or setstate(). state must first have been initialized using initstate() or be the result of a previous call of setstate().
To make the seed different every time we use it, we'd better use time(NULL) as the seed.
For more details:
man 3 rand
man random
2016-04-01
Append:
You can read random numbers from /dev/random or /dev/urandom.
The former may block your process.
So if you are not too sensitive with security, use /dev/urandom.
Random number的更多相关文章
- 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 ...
- Linux shell get random number
the Shell Profile: When a new interactive shell is started, /etc/profile, followed by /etc/bash.bash ...
- Case Study: Random Number Generation(翻译教材)
很荣幸,经过三天的努力.终于把自己翻译的教材做完了,现在把它贴出来,希望能指出其中的不足. Case Study: Random Number Generation Fig. 6.7 C++ 标 ...
- C# random(number)
C#随机函数Random()的用法 出自:http://www.cnblogs.com/wang726zq/archive/2012/04/28/2474711.html http://blog.cs ...
- ISO C Random Number Functions
This section describes the random number functions that are part of the ISO C standard. To use these ...
- LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析
前几天工作需要用LoadRunner进行压力测试,期间对手机号进行参数化设置. 当时选用了<Value>137{Random_quhao}{Unique_weiyi}</Value& ...
- sqlserver datatime value plus random number
If we want to make some identiity value in sqlserver , we can use identity data type in a table.Howe ...
- 文献翻译|Design of True Random Number Generator Based on Multi-stage Feedback Ring Oscillator(基于多级反馈环形振荡器的真随机数发生器设计)
基于多级反馈环形振荡器的真随机数发生器设计 摘要 真随机数生成器(trng)在加密系统中起着重要的作用.本文提出了一种在现场可编程门阵列(FPGA)上生成真随机数的新方法,该方法以 多级反馈环形振荡器 ...
- unity shader random number
http://gamedev.stackexchange.com/questions/32681/random-number-hlsl
随机推荐
- 【HTML5】Canvas图像
把一幅图像放置到画布上, 使用以下方法: drawImage(Img,x,y); 注:这里的Img必须是一个图像对象. 显示一个canvas图像: <!DOCTYPE html> &l ...
- angular源码分析:angular中各种常用函数,比较省代码的各种小技巧
angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...
- windows server2008 安装问题、sqlserver安装设置默认账户问题
1.Bios中的satadata设置开启 2账户和密码最好与本机相同
- elipse + maven + tomcat + springMVC环境搭建
1. java JDK安装 http://jingyan.baidu.com/article/b24f6c82c989da86bfe5dab2.html 2.eclipse安装 http://jing ...
- 制作CocoaPods依赖库最后步骤修改
我是看这篇博客http://blog.csdn.net/wzzvictory/article/details/20067595 做的,但是CocoaPods 2015年4月份有了更新,出现了一个tru ...
- tableView的footerView下面的颜色修改、限制文本框的输入字数
- 【读书笔记】iOS网络-测试与操纵网络流量
一,观测网络流量. 观测网络流量的行为叫做嗅探或数据包分析. 1,嗅探硬件. 从iOS模拟器捕获数据包不需要做特别的硬件或网络配置.如果需要捕获这些数据包,那么可以使用嗅探软件来监听回送设备或是用于连 ...
- OC 多态
OC 多态 多态的概念 没有继承,就没有多态 不同类的对象能够定义相同名称的方法 当父类对象的指针或引用指向子类对象时,会进行动态监测,调用真实的方法 C++只有虚函数才能实现多态,OC中所有方法都可 ...
- iOS 学习 - 14.本地联系人
苹果在iOS9的SDK中废除了AddressBookUI.framework的一些功能(是不是这个库都废除了,有待验证),具体和保存联系人相关的几个系统界面如下:联系人选择:AddressBookUI ...
- Bootstrap分页插件--Bootstrap Paginator
开源中国有一篇介绍的很详细,链接:https://my.oschina.net/shunshun/blog/204587 使用这个插件和使用其他Bootstrap内置的插件一样,需要引入如下文件: & ...