`
betty_betty2008
  • 浏览: 24905 次
  • 性别: Icon_minigender_1
  • 来自: 东莞
最近访客 更多访客>>
社区版块
存档分类
最新评论

(翻译)Phobos 2.029 R部 std.random

    博客分类:
  • D
阅读更多
std.random                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                               
Facilities for random number generation. The old-style functions rand_seed and rand will soon be deprecated as they rely on global state and as such are subjected to various thread-related issues.
随机数产生器。老式的rand_seed 和 rand 很快就会被废弃,因为他们依赖于全局状态,这样就受线程影响。                                                                                                                                                                                                                                                   
The new-style generator objects hold their own state so they are immune of threading issues. The generators feature a number of well-known and well-documented methods of generating random numbers. An overall fast and reliable means to generate random numbers is the Mt19937 generator, which derives its name from "Mersenne Twister with a period of 2 to the power of 19937". In memory-constrained situations, linear congruential generators such as MinstdRand0 and MinstdRand might be useful. The standard library provides an alias Random for whichever generator it considers the most fit for the target environment.
新式的发生器对象拥有自身状态,因而是线程免疫的。新发生器拥有众所周知的、文档良好的生成随机数的方法。一个总体上来说快速而可靠的产生随机数的工具是Mt19937发生器,得名于“自2至19937的平方之间的随机数发生器--马特赛特旋转演算法”。在内存攸关的情况下,线性同余发生器如MinstdRand0 和 MinstdRand会很有用。标准库为每种发生器都提供了别名Random,以根据目标环境选择最合适的发生器。                                                                                                                                                                                                                                                   
Example: 示例:                                                                                                                                                                                                                                                              
// Generate a uniformly-distributed integer in the range [0, 15)
// 在[0,15)区间产生程均匀分布的随机整数                                                                                                                                                                                                                                                   
auto i = uniform(0, 15);


// using a specific random generator
// 用特定的发生器gen;
Random gen;
auto r = uniform(0.0L, 100.0L, gen); // Generate a uniformly-distributed real in the range [0, 100)
                                     // 在[0,100)区间产生程均匀分布的随机实数                                                                                                                                                                                                                                                   
In addition to random number generators, this module features distributions, which skew a generator's output statistical distribution in various ways. So far the uniform distribution for integers and real numbers have been implemented.
除了随机数发生器之外,本模块也提供了分布的功能,以不同方式从侧面反映发生器输出的统计分布状态。目前只实现了整数和实数的均匀分布uniform。
Author:作者:                                                                                                                                                                                                                                                   
Andrei Alexandrescu                                                                                                                                                                                                                                                    
Credits: 鸣谢:                                                                                                                                                                                                                                                              
The entire random number library architecture is derived from the excellent C++0X random number facility proposed by Jens Maurer and contributed to by researchers at the Fermi laboratory.
整个随机数库自C++0X精彩之极的随机数工具派生而来,该工具由Jens Maurer提议并由费米国立加速器实验室(简称费米实验室)贡献。                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                               
struct LinearCongruentialEngine(UIntType,UIntType a,UIntType c,UIntType m);                                                                                                                                                                                                                                                        
Linear Congruential generator.
线性同余发生器。
                                                                                                                                                                                                                                                   
bool hasFixedRange;                                                                                                                                                                                                                                                              
Does this generator have a fixed range? (true).
测试发生器是否拥有一个固定大小的范围(true)。                                                                                                                                                                                                                                                   
UIntType min;                                                                                                                                                                                                                                                               
Lowest generated value (1 if c == 0, 0 otherwise).
产生的最小随机数(如果c==0,其值为1;否则为0)。                                                                                                                                                                                                                                                   
UIntType max;                                                                                                                                                                                                                                                               
Highest generated value (modulus - 1).
产生的最大随机数(modulus - 1)。                                                                                                                                                                                                                                                    
UIntType multiplier;                                                                                                                                                                                                                                                             
UIntType increment;                                                                                                                                                                                                                                                             
UIntType modulus;                                                                                                                                                                                                                                                              
The parameters of this distribution. The random number is x = (x * multipler + increment) % modulus.
本随机数库的方法参数。随机数x 满足以下公式:x= (x * multipler + increment) % modulus.                                                                                                                                                                                                                                                    
this(UIntType x0);                                                                                                                                                                                                                                                              
Constructs a LinearCongruentialEngine generator seeded with x0.
构造种子为x0的线性同余发生器。
                                                                                                                                                                                                                                                   
void seed(UIntType x0 = 1);                                                                                                                                                                                                                                                             
(Re)seeds the generator.
发生器种子。                                                                                                                                                                                                                                                   
void popFront();                                                                                                                                                                                                                                                               
Advances the random sequence.
随机数序列中前进。                                                                                                                                                                                                                                                   
UIntType front();                                                                                                                                                                                                                                                              
Returns the current number in the random sequence.
返回随机数序列中最当前值。                                                                                                                                                                                                                                                   
bool empty;                                                                                                                                                                                                                                                               
Always true (random generators are infinite ranges).
永远为真(随机数发生器是无限range).                                                                                                                                                                                                                                                   
const bool opEquals(LinearCongruentialEngine rhs);                                                                                                                                                                                                                                                           
Compares against rhs for equality.
比较与rhs是否相等。                                                                                                                                                                                                                                                   
alias MinstdRand0;                                                                                                                                                                                                                                                             
alias MinstdRand;                                                                                                                                                                                                                                                              
Define LinearCongruentialEngine generators with well-chosen parameters. MinstdRand0 implements Park and Miller's "minimal standard" generator that uses 16807 for the multiplier. MinstdRand implements a variant that has slightly better spectral behavior by using the multiplier 48271. Both generators are rather simplistic.
用事先选好的参数定义LinearCongruentialEngine发生器。MinstdRand0是Park 和Miller 的以16807为乘数的“最小标准”理论的实现。MinstdRand则以48271为乘数,实现了一个行为上稍稍好些的变数(variant)。两个发生器都足够简单。                                                                                                                                                                                                                                                   
Example: 示例:                                                                                                                                                                                                                                                              
// seed with a constant
// 用常量做种子                                                                                                                                                                                                                                                   
auto rnd0 = MinstdRand0(1);                                                                                                                                                                                                                                                   
auto n = rnd0.popFront; // same for each run//每次运行都会一样                                                                                                                                                                                                                                                   
// Seed with an unpredictable value
// 用一个不可预测的值做种子                                                                                                                                                                                                                                                   
rnd0.seed(unpredictableSeed);                                                                                                                                                                                                                                                   
n = rnd0.popFront; // different across runs//每次运行都会不同                                                                                                                                                                                                                                                   
struct MersenneTwisterEngine(UIntType,size_t w,size_t n,size_t m,size_t r,UIntType a,size_t u,size_t s,UIntType b,size_t t,UIntType c,size_t l);                                                                                                                                                                                                                                                
The Mersenne Twister generator.
马特赛特旋转演算法随机数发生器。
                                                                                                                                                                                                                                                   
size_t wordSize;                                                                                                                                                                                                                                                               
Parameter for the generator.
发生器参数。                                                                                                                                                                                                                                                   
UIntType min;                                                                                                                                                                                                                                                               
Smallest generated value (0).
产生的最小随机数(0)。                                                                                                                                                                                                                                                   
UIntType max;                                                                                                                                                                                                                                                               
Largest generated value.
产生的最大随机数。                                                                                                                                                                                                                                                   
UIntType defaultSeed;                                                                                                                                                                                                                                                              
The default seed value.
默认种子值。                                                                                                                                                                                                                                                   
this(UIntType value);                                                                                                                                                                                                                                                              
Constructs a MersenneTwisterEngine object.
构造马特赛特旋转演算法随机数发生器对象。                                                                                                                                                                                                                                                   
void seed(UIntType value = defaultSeed);                                                                                                                                                                                                                                                            
Seeds a MersenneTwisterEngine object.
给一马特赛特随机数发生器对象施种子。                                                                                                                                                                                                                                                   
void popFront();                                                                                                                                                                                                                                                               
Advances the generator.
前历发生器。                                                                                                                                                                                                                                                   
UIntType front();                                                                                                                                                                                                                                                              
Returns the current random value.
返回当前随机数值。                                                                                                                                                                                                                                                   
bool empty;                                                                                                                                                                                                                                                               
Always false.
恒为假。                                                                                                                                                                                                                                                   
alias Mt19937;                                                                                                                                                                                                                                                               
A MersenneTwisterEngine instantiated with the parameters of the original engine MT19937, generating uniformly-distributed 32-bit numbers with a period of 2 to the power of 19937. Recommended for random number generation unless memory is severely restricted, in which case a LinearCongruentialEngine would be the generator of choice.
用原先的MT19937引擎为参数实例化马特赛特随机数发生器,产生处于2-19937平方之间均匀分布的32位随机数。除非内存攸关,推荐用它产生随机数;当内存攸关时,则应选用线性同余发生器。                                                                                                                                                                                                                                                   
Example:示例:                                                                                                                                                                                                                                                   
// seed with a constant
// 用常量做种子                                                                                                                                                                                                                                                   
Mt19937 gen;                                                                                                                                                                                                                                                   
auto n = gen.front; // same for each run//每次运行都一样                                                                                                                                                                                                                                                   
// Seed with an unpredictable value
// 用一个不可预测的值做种子                                                                                                                                                                                                                                                   
gen.seed(unpredictableSeed);                                                                                                                                                                                                                                                   
n = gen.front; // different across runs//每次运行都不一样                                                                                                                                                                                                                                                   
uint unpredictableSeed();                                                                                                                                                                                                                                                              
A "good" seed for initializing random number engines. Initializing with unpredictableSeed makes engines generate different random number sequences every run.
初始化随机数引擎的很好的种子。用unpredictableSeed做种子每次运行都会产生不同的随机数。                                                                                                                                                                                                                                                   
Example:示例:                                                                                                                                                                                                                                                   
auto rnd = Random(unpredictableSeed);                                                                                                                                                                                                                                                   
auto n = rnd.front;                                                                                                                                                                                                                                                   
...                                                                                                                                                                                                                                                   
alias Random;                                                                                                                                                                                                                                                               
The "default", "favorite", "suggested" random number generator type on the current platform. It is an alias for one of the previously-defined generators. You may want to use it if (1) you need to generate some nice random numbers, and (2) you don't care for the minutiae of the method being used.
当前系统上“默认的”、“最好的”、“推荐的”随机数发生器。是前述随机数发生器的别名。你可在以下情况用它:(1)你想产生一些随机数(2)不在意所用方法的细节。                                                                                                                                                                                                                                                   
Random rndGen();                                                                                                                                                                                                                                                               
Global random number generator used by various functions in this module whenever no generator is specified. It is allocated per-thread and initialized to an unpredictable value for each thread.
在本模块中未明确指定发生器的情况下,被多个方法使用的全局随机数发生器。每个线程单独分配该发生器并以一个不可预测值初始化。                                                                                                                                                                                                                                                   
CommonType!(T1,T2) uniform(string boundaries = "[)", T1, T2, UniformRandomNumberGenerator                                                                                                                                                                                                                                                      
(T1 a, T2 b, ref UniformRandomNumberGenerator urng);                                                                                                                                                                                                                                                    
Generates a number between a and b. The boundaries parameter controls the shape of the interval (open vs. closed on either side). Valid values for boundaries are "[]", "(]", "[)", and "()". The default interval is closed to the left and open to the right.
在数a和b之间产生随机数。参数boundaries控制间隔的形状(左右两边分别为闭合还是开放)。boundaries的有效值有“[]”、“(]”、“[)”、“()”。默认值是左闭右开。                                                                                                                                                                                                                                                   
Example:示例:                                                                                                                                                                                                                                                   
Random gen(unpredictableSeed);                                                                                                                                                                                                                                                   
// Generate an integer in [0, 1023]
// [0,1013]之间产生随机整数。                                                                                                                                                                                                                                                   
auto a = uniform(0, 1024, gen);                                                                                                                                                                                                                                                   
// Generate a float in [0, 1)
// [0,1]之间产生浮点数。                                                                                                                                                                                                                                                   
auto a = uniform(0.0f, 1.0f, gen);                                                                                                                                                                                                                                                   
CommonType!(T1,T2) uniform(string boundaries = "[)", T1, T2                                                                                                                                                                                                                                                         
(T1 a, T2 b); )                                                                                                                                                                                                                                                    
As above, but uses the default generator rndGen.
同上,但用的是默认发生器rndGen.                                                                                                                                                                                                                                                   
F[] uniformDistribution(F = double)(size_t n, F[] useThis = null);                                                                                                                                                                                                                                                         
Generates a uniform probability distribution of size n, i.e., an array of size n of positive numbers of type F that sum to 1. If useThis is provided, it is used as storage.
生成大小为n的均匀概率分布。亦即一个有n个正F型元素的数组,各元素总合为1。如果提供了useThis,则useThis当做存贮容器。                                                                                                                                                                                                                                                   
void randomShuffle(Range, RandomGen = Random)(Range r, ref RandomGen gen = rndGen);                                                                                                                                                                                                                                                       
Shuffles elements of r using r as a shuffler. r must be a random-access range with length.
对Range r 洗牌,r 必须为一已知长度的随机访问区间。                                                                                                                                                                                                                                                   
size_t dice(R)(ref R rnd, double[] proportions...);                                                                                                                                                                                                                                                           
Rolls a dice with relative probabilities stored in proportions. Returns the index in proportions that was chosen.
按存放在proportions[] 中的相关值可能性比例掷骰子。返回选中值在proportions[]中的索引。                                                                                                                                                                                                                                                   
Example:示例:                                                                                                                                                                                                                                                   
auto x = dice(0.5, 0.5);   // x is 0 or 1 in equal proportions// x 为0 或1 的概率相等,各50%                                                                                                                                                                                                                                                   
auto y = dice(50, 50);     // y is 0 or 1 in equal proportions// y 为0 或1 的概率相等,各50%                                                                                                                                                                                                                                                   
auto z = dice(70, 20, 10); // z is 0 70% of the time, 1 30% of the time,                                                                                                                                                                                                                                                   
                           // and 2 10% of the time
                           // z 为0的概率是70%,为1的概率是20%,为2 的概率是10%。                                                                                                                                                                                                                                                   
struct RandomCover(Range,Random);                                                                                                                                                                                                                                                            
RandomCover!(Range,Random) randomCover(Range, Random)(Range r, Random rnd);                                                                                                                                                                                                                                                        
Covers a given range r in a random manner, i.e. goes through each element of r once and only once, just in a random order. r must be a forward access range with length.
Range r 的一次性随机顺序遍历,即对于r 按随机顺序遍历,且仅遍历一次。r必须为一已知长度的随机方问区间。                                                                                                                                                                                                                                                   
Example:示例:                                                                                                                                                                                                                                                   
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ];                                                                                                                                                                                                                                                   
auto rnd = Random(unpredictableSeed);                                                                                                                                                                                                                                                   
foreach (e; randomCover(a, rnd))                                                                                                                                                                                                                                                   
{                                                                                                                                                                                                                                                   
    writeln(e);                                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                   
deprecated void rand_seed(uint seed, uint index);                                                                                                                                                                                                                                                           
The random number generator is seeded at program startup with a random value. This ensures that each program generates a different sequence of random numbers. To generate a repeatable sequence, use rand_seed() to start the sequence. seed and index start it, and each successive value increments index. This means that the nth random number of the sequence can be directly generated by passing index + n to rand_seed().
随机数发生器在程序一开始即确定了具有随机值的种子。这样可确保程序每一次运行时生成不同的随机数序列。要想生成可重复序列,用rand_seed()。用seed index的话,每个后续值都会使index的值增加。这意味着序列中的第n个随机数可把index+n传递给rand_seed()直接生成。                   
Note:需知:                                                                                                                                                                                                                                                   
This is more random, but slower, than C's rand() function. To use C's rand() instead, import std.c.stdlib.
这个更随机一些,但要比C库的rand()要慢一点。要用C库的rand(),请导入std.c.stdlib.                   
BUGS:                   
Shares a global single state, not multithreaded. SCHEDULED FOR DEPRECATION.
共享一个全局单一状态,非多线程的。计划废弃。                   
deprecated uint rand();                                                                                                                                                                                                                                                              
Get the popFront random number in sequence.
返回序列中首个随机数。                   
BUGS:                   
Shares a global single state, not multithreaded. SCHEDULED FOR DEPRECATION.
共享一个全局单一状态,非多线程的。计划废弃。                   
             
分享到:
评论
2 楼 betty_betty2008 2009-04-29  
这几篇很少人需要看的,重在参与,呵呵。
1 楼 hqs7636 2009-04-28  
进度很快啊,我一篇都还没翻完

相关推荐

    数字图像处理(冈萨雷斯)第三版---图片数据库-第3章

    2. **图像的数字化**:包括采样和量化两个步骤。采样决定了图像的空间分辨率,量化则决定了图像的灰度或色彩级别。 3. **图像的表示与存储**:例如,用二维数组表示图像,常见的有RGB模型和灰度级表示。 4. **图像...

    Petabridge.Phobos.Web.InfluxDb:使用InfluxDb启用Phobos的Akka.NET + ASP.NET Core应用程序

    Petabridge.Phobos.Web.InfluxDB 该存储库源自我们在使用的代码所有内容,除了我们选择的外,其他内容大致相同。 注意:此解决方案使用 的 ,您可以在以下位置通过Grafana Cloud将其安装在自己的应用程序中: ://...

    Phobos勒索专杀工具

    Phobos勒索专杀工具是一款专门针对名为Phobos的勒索病毒设计的安全软件。Phobos勒索病毒是一种恶意软件,它通过加密用户的文件并要求支付赎金来解锁,对个人和企业的数据安全构成了严重威胁。这款专杀工具的出现,...

    Phobos:兼容Ares的C&C红色警报2

    Phobos是一个WIP社区项目,它基于和为Yuri's Revenge提供了一组新功能和修复程序,以允许注入代码。它旨在陪伴而不是取代它,因此不会引入不兼容性。 现在,您可以 (这是我的C&C改装服务器)上的专用讨论该项目。 ...

    .2700解密工具 phobos解密工具

    .2700勒索病毒解密工具 phobos解密工具,需要密钥才能解密。上次交了5万元给的解密工具,但是每个机器秘密是不一样的。这是工具!

    Phobos

    Phobos是一款与字体相关的压缩包,其名称来源于火星的最大卫星——“火卫一”Phobos。在IT行业中,字体扮演着至关重要的角色,它不仅关乎文本的可读性,还影响着用户界面的美观和用户体验。让我们深入探讨一下Phobos...

    netty in actin

    ### Netty In Action 知识点详述 #### 一、为什么使用Netty? ##### 1.1 高性能与简单易用 - **间接解决问题**:Netty作为一款NIO客户端-服务器框架,遵循David John Wheeler提出的观点——通过添加额外的逻辑层...

    Python库 | phobos-1.0.1-py3-none-any.whl

    **Python库Phobos介绍** Phobos是一个Python库,主要设计用于提高开发者的生产力,尤其是在处理数据和构建复杂系统时。它的名字来源于希腊神话中的恐惧之神,寓意着它可以帮助开发者克服编程过程中的种种困难。这个...

    d语言,dmd.2.042

    8. **丰富的库支持**:D语言有广泛的库支持,包括标准库Phobos和第三方库Druntime,覆盖了网络、图形、文件I/O等多个领域。 9. **简洁语法**:D语言的语法设计借鉴了其他语言,力求简洁明了,易于理解和学习。 10....

    phobos软件

    Phobos是一款专门用于检测SSR(Simple Sequence Repeat)微卫星的生物信息学软件。SSR,也称为短串联重复序列,是DNA中的一种常见变异形式,常用于遗传标记和基因定位研究。Phobos软件的出现极大地简化了对这些序列...

    GandCrab v5.0.x 那些事儿——行为分析、免疫与救援须知

    GandCrab v5.0.x 那些事儿——行为分析、免疫与救援须知

    phobos:MarsRon的Discord.js机器人

    【标题】"Phobos: MarsRon的Discord.js机器人" Phobos是MarsRon创建的一款基于Discord.js库的机器人,它专为Discord社区设计,提供了丰富的交互功能和自动化服务。Discord.js是一个强大的JavaScript库,使得开发者...

    dasocks:D 中的异步网络库

    dasocks 使用标准库中的以下模块完全用 D 编写: core.thread、std.socket、std.c.string(标准 C 库)、std.array、std.conv、std.string dasocks 具有以下特点简化的异步socket使用,跨平台,线程管理,使用字符...

    itunes:iTunes API周围的Ruby包装器,可让您搜索iTunes商店中可用的任何类型的数据

    的iTunes iTunes API周围的Ruby包装器,可让您搜索iTunes商店中可用的任何类型的数据。 范例回应 { " artist_id " : 954266 , ... " artwork_url30 " : " http://a1.phobos.apple.com/us/r1000/049/Features

    html5飞机打字小游戏源码.zip

    `phobos.png` 应该是一个图像文件,可能是游戏中的角色、背景或者游戏元素的图形资源。HTML5的Canvas API可以用来绘制和操作这些图像,从而实现动态的游戏画面。开发者可以通过更换或编辑这个图像来改变游戏的视觉...

    Phobos_1.5.4-BUILDABLE-SRC:Phobos 1.5.4源代码可构建

    在本文中,我们将深入探讨 Phobos 1.5.4 的核心特性、构建过程以及与 Java 语言的关系。 首先,让我们了解一下 Phobos。Phobos 是一个可能是框架或库的名称,它可能被设计用于特定的软件开发需求。"1.5.4" 表示这是...

    编译时开发D语言

    D语言的标准库(Phobos)包含了丰富的模块,涵盖了I/O、网络、文件操作、数据结构、算法等多个方面。其中,`std.meta`和`std.traits`模块提供了编译时编程所需的功能,而`std.range`和`std.functional`则提供了函数...

    phobos-开源

    2. **代码生成**:框架包含工具,能够自动生成类和Web表单,减少了手动编写重复代码的时间。这不仅提高了开发速度,也降低了出错的可能性。 3. **对象关系映射(ORM)**:Phobos提供了持久性层,通过ORM技术将...

    Phobos-1.9.0-BUILDABLE-SRC:Phobos 1.9.0清洁可建SRC

    [CLEAN] Phobos 1.9.0 Buildable SRC&Release 此资源已由两个人(本人和一个朋友)检查过。 我的朋友短暂浏览文件时没有找到任何东西。 我已经扫描并遍历了每个文件和lib,并更换了它们以确保它是干净的。 这是...

Global site tag (gtag.js) - Google Analytics