[0.0,1.0)范围内的double的唯一值总数是多少? [英] What is the total number of unique values for a double in the range [0.0, 1.0)?

查看:122
本文介绍了[0.0,1.0)范围内的double的唯一值总数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Random.NextDouble()(在[0.0,1.0]范围内的Double)有时会与一个较大的Int64相乘(让Int64 big = 9000000000L),并且对结果进行累加以获得大于该值的随机Int64值.从Random.Next()(范围为[0,Int32.MaxValue)的Int32)获得.

Random.NextDouble() (a Double from the range [0.0,1.0)) is sometimes multiplied with a large Int64 (let Int64 big = 9000000000L), and the result floored to obtain a random Int64 value larger than what can be obtained from Random.Next() (an Int32 from the range [0,Int32.MaxValue)).

Random r = new Random();
long big = 9000000000L;
long answer = (long) (r.NextDouble() * big);

在我看来,Double的唯一值总数在[0.0,1.0)范围内提供了它可能生成的唯一Int64数量的上限.实际上,由于许多不同的Double会映射到同一个Int64,因此松散的上限.

It seems to me that the total number of unique values for a Double in the range [0.0, 1.0) provides an upper-bound for the number of unique Int64 it can possibly generate. A loose upper-bound, in fact, as many different Doubles will map to the same Int64.

因此,我想知道:[0.0,1.0)范围内的double的唯一值总数是多少?

Hence, I would like to know: what is the total number of unique values for a double in the range [0.0, 1.0)?

更好的办法是告诉我"big"可以取的最大值是多少,以便"answer"可以是[0,big)范围内的值,以及"answer"的值分布是否均匀,假设Random.NextDouble()是统一的.

Even better if you can tell me what is the largest value "big" can take so that "answer" can be a value from the range [0,big), and whether the distribution of values of "answer" is uniform, assuming that Random.NextDouble() is uniform.

Double(double)在这里是指IEEE 754浮点双精度,而Int64(long)和Int32(int)分别是指64位和32位有符号2的补码.

Double (double) here refers to IEEE 754 floating-point double, while Int64 (long) and Int32 (int) refer to 64-bit and 32-bit signed 2's complement respectively.

受此问题启发:在Java中生成10位唯一的随机数

虽然我使用C#,但该问题与语言无关,并且与离散数学有关,而与编程无关,但它主要是出于对数学的好奇心而困扰我,而是让程序员仅在想使用公式的情况下才对此感到困扰从安全的角度来看应该做的事.

While I used C#, this question is language-agnostic and is more about discrete mathematics than programming, but it bothers me not mainly from a sense of mathematical curiousity, but from that of a programmer wanting to use a formula only if it does what it is supposed to do and from a security viewpoint.

推荐答案

作为您问题的推论,我将告诉您Random C#生成器在内部使用了一个生成器,该生成器将0...Int32.MaxValue - 1之间的数字赋予他" .然后将数字除以Int32.MaxValue(从技术上乘以该数字的倒数)以返回双精度数.因此,在C#中,仅返回Int32.MaxValue个可能的双精度(0...Int32.MaxValue - 1)

As a corollary to your question, I'll tell you that the Random C# generator uses internally a generator that "gives him" numbers between 0...Int32.MaxValue - 1. Then it divides the number by Int32.MaxValue (technically it multiplies by the inverse of that number) to return a double. So in C#, there are only Int32.MaxValue possible doubles returned (0...Int32.MaxValue - 1)

这篇关于[0.0,1.0)范围内的double的唯一值总数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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