javascript - Math.random number of random bits - Stack Overflow

Is there any requirement on how many random bits Math.random is supposed to produce? I did some tests o

Is there any requirement on how many random bits Math.random is supposed to produce? I did some tests on Chrome and Firefox's implementations, converting the results to hex to examine the bits, and Firefox 27.0.1 gives results like

0x1.de619579d56f3p-1
0x1.ef1ada9306decp-2
0x1.df3b75e208ce6p-1

whereas Chrome Version 33.0.1750.154 m gives

0x1.1190f39c00000p-2
0x1.b959e3b600000p-1
0x1.90f614b400000p-2

which is godawful in parison. It appears to be a 32-bit result, whereas Firefox's values seem to use 53 random bits.

Is there any requirement on how many random bits Math.random is supposed to produce? I did some tests on Chrome and Firefox's implementations, converting the results to hex to examine the bits, and Firefox 27.0.1 gives results like

0x1.de619579d56f3p-1
0x1.ef1ada9306decp-2
0x1.df3b75e208ce6p-1

whereas Chrome Version 33.0.1750.154 m gives

0x1.1190f39c00000p-2
0x1.b959e3b600000p-1
0x1.90f614b400000p-2

which is godawful in parison. It appears to be a 32-bit result, whereas Firefox's values seem to use 53 random bits.

Share Improve this question asked Mar 23, 2014 at 11:16 user2357112user2357112 284k31 gold badges483 silver badges565 bronze badges 3
  • I have never seen numbers being represented that way. Is that just the hex representation of the decimal number? – thefourtheye Commented Mar 23, 2014 at 11:30
  • @thefourtheye: See the specification of the format. I used Python (didn't see a native Javascript tool for the conversion), but the same format is also used in C and Java. The part between the x and the p is a hexadecimal number, and the part after the p is a decimal representing a power of two to multiply it by. – user2357112 Commented Mar 23, 2014 at 11:34
  • 1 Oh, thanks man. But the Math.random()'s spec doesn't say anything about it. So, it actually depends on the implemenetation I believe. – thefourtheye Commented Mar 23, 2014 at 11:37
Add a ment  | 

2 Answers 2

Reset to default 4

http://www.ecma-international/ecma-262/5.1/#sec-15.8.2.14

15.8.2.14 random ( )

Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.

Seems to be all the spec says.

https://v8.dev/blog/math-random says Chrome 49 uses a random number generator called "xorshift128+" and I think Firefox does too. This generates 64-bit results (converted to floats, which I think means 53 bits of precission) with a period of 2^128-1.

https://lwn/Articles/666407/ has some more history of the browsers' RNGs.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425796a4627169.html

相关推荐

  • javascript - Math.random number of random bits - Stack Overflow

    Is there any requirement on how many random bits Math.random is supposed to produce? I did some tests o

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信