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的更多相关文章

  1. 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 ...

  2. Linux shell get random number

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

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

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

  4. C# random(number)

    C#随机函数Random()的用法 出自:http://www.cnblogs.com/wang726zq/archive/2012/04/28/2474711.html http://blog.cs ...

  5. ISO C Random Number Functions

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

  6. LoadRunner压力测试之Unique Number参数类型、Random Number参数类型浅析

    前几天工作需要用LoadRunner进行压力测试,期间对手机号进行参数化设置. 当时选用了<Value>137{Random_quhao}{Unique_weiyi}</Value& ...

  7. 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 ...

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

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

  9. unity shader random number

    http://gamedev.stackexchange.com/questions/32681/random-number-hlsl

随机推荐

  1. [deviceone开发]-QQ分享、微信分享和新浪微博分享

    一.简介 该demo主要实现QQ分享.微信分享和新浪微博分享.(调试包请到论坛扫描对应二维码下载) 二.效果图 三.相关讨论 http://bbs.deviceone.net/forum.php?mo ...

  2. 怎么把多个GridView和Repeater导入到word或者excel中

    最近开发网上商城有发现我们会涉及到订单打印的问题,本来想到直接显示打印即可但是考虑还得下载到本地,最后只有直接选择控件按流方式输出. 导入到word中代码如下: protected void Butt ...

  3. AngularJS1.3一些技巧

    前言 框架选择.在上一篇文章评论中,有人说angular1.3是个过时的东西,建议使用angular2.其实这种说法很像拿jQuery1.x和jQuery2.x做比较,新的版本当然会有优化优势的地方, ...

  4. ABAP 指針常用语法

    1 .定义指針 :指針的定義主 要有以下語句 定義任意類型的指針,但是不具備欄位結構(僅僅是一個地址) FIELD-SYMBOLS <carrid> TYPE ANY. 參考數據庫表定義( ...

  5. 刷新ALV定位到当前记录行

    如果使用"REFRESH_TABLE_DISPLAY"刷新ALV后,记录会跳到第一行,以下代码可以使记录仍然定位在当前行 DATA ls_stable TYPE lvc_s_stb ...

  6. 为 MDS 修改 SharePoint 2013组件

    了解如何修改 SharePoint 项目中的组件以在 SharePoint 2013 中利用最少下载策略(MDS).   本文内容 为何修改 SharePoint 组件? 母版页 ASP.NET 页面 ...

  7. 编译安装mysql(Ubuntu10 64位)

    选用较好的编译器和较好的编译器选项,这样应用可提高性能10-30%,这个对大多数程序都非常重要 Mysql的编译,不同的版本具体的配置方式是有差别的 旧版的配置形式参考 这个形式主要是使用config ...

  8. 【代码笔记】iOS-使图片两边不拉伸,中间拉伸

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  9. 什么是XMLA-- XML for Analysis

    在我刚开始接触OLAP时,同事就告诉我 XMLA会让他使用更方便. 什么是XMLA? Providers 供应商 ActivePivot Hyperion Essbase IBM Infosphere ...

  10. WebService的介绍概念 收藏

    WebService学习总结(二)——WebService相关概念介绍 一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨 ...