C++ Standard-Library Random Numbers
Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed.
The random-number library generates random numbers through a set of cooperating classes: random-number engines and random-number distribution classes. An engine generates a sequence of unsigned random numbers. A distribution uses an engine to generate random numbers of a specified type, in a given range, distributed according to a particular probability distribution. A random-number generator means the combination of a distribution object with an engine.
The random-number engines and random-number distribution are both function-object classes. The random-number engines define a call operator that takes no arguments and returns a random unsigned numbers. We can generate raw random numbers by calling an object of a random-number engine type. The library defines several random-number engines that differ in terms of their performance and quality of randomness. Each compiler designates one of these engines as the defaut_random_engine type. This type is intended to be the engine with the most generally useful properties. The distribution types define a call operator that takes a random-number engine as its argument. The distribution object uses its engine argument to produce random numbers that the distribution object maps to the specified distribution.
A given random-number generator always produces the same sequence of numbers. A function with a local random-number generator should make that generator (both the engine and distribution objects) static. Otherwise, the function will generate the identical sequence on each call.
The fact that a generator returns the same sequence of numbers is helpful during debugging. However, once our program is tested, we often want to cause each run of the program to generate different random results. We do so by providing a seed. A seed is a value that an engine can use to start generating numbers at a new point in its sequnce.
We can seed an engine in one of two ways:
- provide the seed when creating an engine object
- call the engine's seed member
Pickintg a good seed, like most things about generating good random numbers, is surprisingly hard. Perhaps the most common approach is to call the system time function. This function, defined in the <ctime> header, returns the number of seconds since a given epoch. The time function takes a single parameter that is a pointer to structure into which to write the time. If that pointer is null, the function just returns the time. Because time returns time as the number of seconds, this seed is useful only for applications that generate the seed at second-level, or longer, intervals.
C++ Standard-Library Random Numbers的更多相关文章
- Generating Gaussian Random Numbers(转)
Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about th ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- C++ Standard Library
C++ Standard Library *注:内容主要是对參考1的学习记录.知识点与图片大都来源于该书, 部分知识点与图片来源于參考2. 详细參考信息,见最下方參考. * C++98中新支持的语言特 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- Macro definition of snprintf conflicts with Standard Library function declaration
Macro definition of snprintf conflicts with Standard Library function declaration 即将此处的宏定义注释掉,因为在VS2 ...
随机推荐
- WebResource-asp.net自定义控件引用外部资源方法
rom:http://www.lmwlove.com/ac/ID879 在asp.net中开发自定义控件时,如果我们要用到图片,外部css,js等文件,那么最好的方式就是将这些文件作为自定义控件嵌入的 ...
- ArcGis 统计方法
from:http://blog.sina.com.cn/s/blog_4177d50b0100fjbg.html 概述 一般常用的统计功能例如:唯一字段统计.数据行数统计.数据值求和统计等. 1.基 ...
- MVC3中,在control里面三种Html代码输出形式
MVC3中,在control里面三种Html代码输出形式:ViewData["msg"] = "<br /> Title <br />" ...
- jade 渲染js片段
script. str = !{JSON.stringify(val)}; res.render('wxpay', {val:result});
- [转]World Wind Java开发之五——读取本地shp文件
World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...
- opencv3中的机器学习算法之:EM算法
不同于其它的机器学习模型,EM算法是一种非监督的学习算法,它的输入数据事先不需要进行标注.相反,该算法从给定的样本集中,能计算出高斯混和参数的最大似然估计.也能得到每个样本对应的标注值,类似于kmea ...
- 三层ViewPager嵌套 的事件处理
这么多ViewPager嵌套在一起肯定会遇到冲突 不信你试试(笑脸) 下面来说怎么解决.....太为难我这个菜b了 设置外部的父控件不要拦截我子控件的事件,通过重写ViewPager的 @Overri ...
- JDSideMenu实现(整块)侧滑功能,主视图会和状态栏(StatusBar)会一起滑动。
JDSideMenu 实现侧边菜单功能,支持手势滑动.跟一般的侧边菜单不一样的是,滑动主视图,主视图会和状态栏(StatusBar)会一起滑动. demo 自行下载
- SVPullToRefresh 下拉刷新,上拉加载
https://github.com/Sephiroth87/ODRefreshControl 类似刷新控件,类似qq动画的那种刷新. 一.下载第三方库 https://github.com/samv ...
- MBProgressHUD框架的使用:https://github.com/jdg/MBProgressHUD
MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBP ...