博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pseudo-random sequence generation
阅读量:5914 次
发布时间:2019-06-19

本文共 3704 字,大约阅读时间需要 12 分钟。

rand

Generate random number (function)

srand

Initialize random number generator (functions)

c/c++产生随机数需要用到c库中的两个函数:srand和rand。srand函数用来设置随机数种子,初始化随机数生成器;rand函数用来产生随机数。两个函数的介绍和使用示例如下:

srand

function

void srand ( unsigned int seed );

Initialize random number generator

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header <ctime>) is different each second, which is distinctive enough for most randoming needs.

Parameters

seed: An integer value to be used as seed by the pseudo-random number generator algorithm.

Example

/* srand example */#include 
#include
#include
int main (){ printf ("First number: %d\n", rand() % 100); srand ( time(NULL) ); printf ("Random number: %d\n", rand() % 100); srand ( 1 ); printf ("Again the first number: %d\n", rand() %100); return 0;}

Output:

First number: 41

Random number: 13

Again the first number: 4

rand

function

int rand ( void );

Generate random number

Returns a pseudo-random integral number in the range 0 to RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in <cstdlib>. Its default value may vary between implementations but it is granted to be at least 32767.

A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span(跨度) and add the initial value of the range:

( value % 100 ) is in the range 0 to 99

( value % 100 + 1 ) is in the range 1 to 100

( value % 30 + 1985 ) is in the range 1985 to 2014

Notice though that this modulo operation does not generate a truly uniformly distributed(均匀分布) random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

Return Value

An integer value between 0 and RAND_MAX.

Example (一个简单的猜数游戏)

/* rand example: guess the number */#include 
#include
#include
int main (){ int iSecret, iGuess; /* initialize random seed: */ srand ( time(NULL) ); /* generate secret number: */ iSecret = rand() % 10 + 1; do { printf ("Guess the number (1 to 10): "); scanf ("%d",&iGuess); if (iSecret
iGuess) puts ("The secret number is higher"); } while (iSecret!=iGuess); puts ("Congratulations!"); return 0;}

Output

Guess the number (1 to 10): 5

The secret number is higher

Guess the number (1 to 10): 8

The secret number is lower

Guess the number (1 to 10): 7

Congratulations!

In this example, the random seed is initialized to a value representing the second in which the program is executed (time is defined in the header <ctime>). This way to initialize the seed is generally a good enough option for most randoming needs.

 

转载地址:http://afgpx.baihongyu.com/

你可能感兴趣的文章
终极指南:如何使用Visual Studio Code进行 Java 开发?
查看>>
通过源码解析 Node.js 中一个 HTTP 请求到响应的历程
查看>>
做了一点事,学到了一些
查看>>
CodeIgniter的密码处理论
查看>>
深入Mysql - 谈谈我对数据类型的认识
查看>>
Tsuru 1.7.0-rc4 发布,基于 Docker 的 PaaS 框架
查看>>
Apache HBase 2.1.3 发布,分布式数据库
查看>>
微信端H5开发整体解决方案
查看>>
Python之retrying
查看>>
OWASP 10 大 Web 安全问题在 JEE 体系完全失控
查看>>
洛谷 P1640 BZOJ 1854 [SCOI2010]连续攻击游戏
查看>>
如何理解Unity组件化开发模式
查看>>
util.promisify 的那些事儿
查看>>
未来黑科技公司完成亿元Pre-B轮融资,已和宝马达成合作
查看>>
三篇文章了解 TiDB 技术内幕 - 谈调度
查看>>
【DG】DG的3种保护模式
查看>>
[20150107]关于print_table.txt
查看>>
Chrome 如何知道网站启用了SPDY 协议?
查看>>
8天玩转并行开发——第五天 同步机制(下)
查看>>
一次性关闭所有的Activity
查看>>