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
随机推荐
- 如何垂直居中div?面试经常问到
水平居中:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto;} 让绝对定位的div居中 ;;;;} 重点来了! 水平垂直居中 ...
- HTML5移动开发学习笔记
最近做webapp项目过程中,发现坑还是挺多的,以下是网络收集一些开发中的常见问题及知识汇总,以便查阅,慢慢更新:). meta 基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 < ...
- SAP打印机配置
SAP打印机配置 一.SAP打印原理 SAP的打印过程分两个步骤: 1.创建假脱机请求: 2.创建输出请求: 在点击打印按钮后,系统会提示创建假脱机请求后,你可以选择直接生成输出请求,或者手动生成输出 ...
- Sharepoint 2010 工作流启动时处理表单出错
问题: Shareoint 2010 列表工作流启动时,显示“处理表单时出现严重错误”. Error Message-1: Object doesn't support property or met ...
- Map集合概述
java集合最后一站之Map,给自己的总结画个句号... Map用于保存具有映射关系的数据. 1.HashMap和Hashtable实现类 HashMap和Hashtable都是Map接口的典型实现类 ...
- 交换两个变量的值swap(a,b)
方法一:使用第三方变量 , b = , temp; temp = a; a = b; b = temp; 方法二:变量加减法(即121,加减减) , b = ; a = a + b; b = a - ...
- Android 动态创建Fragment
Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...
- 【工具相关】iOS-Reveal的使用
一,首先下载Reveal工具. Reveal的下载地址:http://revealapp.com/download. 下载界面如下图所示,选择Download Trail蓝色按钮可以进行下载: 二,新 ...
- 微信企业号api调用频率
主动调用的频率限制 当你获取到AccessToken时,你的应用就可以成功调用企业号后台所提供的各种接口以管理或访问企业号后台的资源或给企业号成员发消息. 为了防止企业应用的程序错误而引发企业号服务器 ...
- java重载和重写的区别
一.重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现 ...