生成(伪)(U)的Int64和十进制的随机值的限制 [英] Generation of (pseudo) random constrained values of (U)Int64 and Decimal

查看:240
本文介绍了生成(伪)(U)的Int64和十进制的随机值的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:为了简洁起见,下面不会随机性和伪随机性之间的辨别。另外,在此背景下,受限是指给出最小值和最大值之间')

Note: For brevity's sake, the following will not discern between randomness and pseudo-randomness. Also, in this context, 'constrained' means 'between given min and max values')

System.Random类提供了随机生成整数,双打和字节数组。
使用Random.Next,人们可以很容易产生布尔类型,字符,(S)字节,(U)的Int16,(U)的Int32随机约束值。使用Random.NextDouble(),可以产生同样类型的双人和单人的约束值(据我这种类型的理解去)。随机串产生的 解决 之前。

The System.Random class provides random generation of integers, doubles and byte arrays. Using Random.Next, one can easily generate random constrained values of type Boolean, Char, (S)Byte, (U)Int16, (U)Int32. Using Random.NextDouble(), one can similarly generate constrained values of types Double and Single (as far as my understanding of this type goes). Random string generation (of a given length and alphabet) has also been tackled before.

考虑剩下的基本数据类型(不包括对象):十进制和(U)的Int64。他们随机生成已经被攻克,以及(十进制,的(U)Int64 [使用Random.NextBytes()]),但不约束时。拒绝采样(即,循环,直到所产生的值是在所需的范围)理论上可以使用,但它显然不是一个实用的解决方案。 。正火NextDouble()不会工作,因为它没有足够的显著数字

Consider the remaining primitive data types (excluding Object): Decimal and (U)Int64. Their random generation has been tackled as well (Decimal, (U)Int64 [using Random.NextBytes()]), but not when constrained. Rejection sampling (i.e. looping until the generated value is the the desired range) could theoretically be used, but it is obviously not a practical solution. Normalizing NextDouble() won't work because it doesn't have enough significant digits.

总之,我要求正确执行以下功能:

In short, I am asking for the proper implementation of the following functions:

long NextLong(long min, long max)
long NextDecimal(decimal min, decimal max)

请注意,由于System.DateTime的是基于一个ulong,则第一功能将允许随机约束生成这样结构,以及(类似于这里 ,只有在蜱,而不是分钟)。

Note that, since System.DateTime is based on a ulong, the first function would allow for random constrained generation of such structs as well (similar to here, only in ticks instead of minutes).

在此先感谢!

推荐答案

让我们假设你懂得产生N个随机位。这或者使用的nextBytes 或适当的限制重复调用 Random.Next 很容易地完成。

Let's assume you know how to generate N random bits. This is pretty easily done either using NextBytes or repeated calls to Random.Next with appropriate limits.

要产生的权利范围内的长/ ULONG,制定出的范围有多大,它有多少位需要来代表它。然后,您可以用拒绝抽样这将的在最坏的情况的拒绝一半生成的值(例如,如果你想在区间[0,128]的数值,这意味着你产生[0,255]多次)。如果你想要一个非零的基础范围,只是制定出范围的大小,生成[0,大小)一个随机值,然后添加了基础。

To generate a long/ulong in the right range, work out how large the range is, and how many bits it takes to represent it. You can then use rejection sampling which will at worst reject half the generated values (e.g. if you want a value in the range [0, 128], which means you'll generate [0, 255] multiple times). If you want a non-zero based range, just work out the size of the range, generate a random value in [0, size) and then add the base.

生成一个随机的小数为signficantly困难,我相信 - 除了别的,你必须指定你想要的分布

Generating a random decimal is signficantly harder, I believe - aside from anything else, you'd have to specify the distribution you wanted.

这篇关于生成(伪)(U)的Int64和十进制的随机值的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆