短い答え:範囲は[0、1)あります。
はい。 Float
ためRandom
の実装は[source]ある:
instance Random Float where
randomR = randomRFloating
random rng =
-- TODO: Faster to just use 'next' IF it generates enough bits of randomness.
case random rng of
(x,rng') ->
-- We use 24 bits of randomness corresponding to the 24 bit significand:
((fromIntegral (mask24 .&. (x::Int32)) :: Float)
/fromIntegral twoto24, rng')
-- Note, encodeFloat is another option, but I'm not seeing slightly
-- worse performance with the following [2011.06.25]:
-- (encodeFloat rand (-24), rng')
where
mask24 = twoto24 - 1
twoto24 = (2::Int32)^(24::Int32)
それはランダム32ビット整数x
(ゼロが可能な値である)最初の8ビットのうち、そのマスクを使用し、2によってその値を除算します。その結果、範囲は0(インクルード)〜1(除外)になります。それが表すことができる最大値は0.999999940395
です。
このように動作する理由は、Float
に24ビットのmantisse(7ビットの指数と符号ビット)があるためです。その範囲で変換することで、Float
の値が等しくなることが保証されます:最後の24ビットが最初にFloat
のmantisseにコピーされ、floatが正規化され、指数が変更されて[0 、1)範囲。
詳細な回答ありがとうございます、私はあなたがソースにアクセスできるかどうかも知りませんでした! – flawr