[commit: packages/random] new_api: Minor: comment tweak, DEVLOG notes, remved cruft. (797eb35)
git at git.haskell.org
git at git.haskell.org
Thu Mar 19 15:44:01 UTC 2015
Repository : ssh://git@git.haskell.org/random
On branch : new_api
Link : http://git.haskell.org/packages/random.git/commitdiff/797eb35eb26f2fb18aadbdb757a1b6f0bbb099fe
>---------------------------------------------------------------
commit 797eb35eb26f2fb18aadbdb757a1b6f0bbb099fe
Author: Ryan Newton <rrnewton at gmail.com>
Date: Mon Jun 27 13:47:54 2011 -0400
Minor: comment tweak, DEVLOG notes, remved cruft.
>---------------------------------------------------------------
797eb35eb26f2fb18aadbdb757a1b6f0bbb099fe
DEVLOG.md | 28 ++++++++++++++++++++++++++++
System/Random.hs | 44 ++++++++++++++------------------------------
2 files changed, 42 insertions(+), 30 deletions(-)
diff --git a/DEVLOG.md b/DEVLOG.md
index b482f1f..24972cf 100644
--- a/DEVLOG.md
+++ b/DEVLOG.md
@@ -202,3 +202,31 @@ improved. And in spite of that it didn't slow down TOO much. Also,
randomIvalBits can fix the problems in tickets #5278 and #5280 having
to do with uniformity and assumptions about the generators.
+
+Oops, there were some bugs. Here are new times as of revision 3581598e57ef995f:
+
+ Next timing range-restricted System.Random.randomR:
+ 3,738,614 randoms generated [System.Random Ints] ~ 892 cycles/int
+ 7,516,652 randoms generated [System.Random Word16s] ~ 444 cycles/int
+ 110,307 randoms generated [System.Random Floats] ~ 30,234 cycles/int
+ 110,507 randoms generated [System.Random CFloats] ~ 30,179 cycles/int
+ 2,538,000 randoms generated [System.Random Doubles] ~ 1,314 cycles/int
+ 108,386 randoms generated [System.Random CDoubles] ~ 30,770 cycles/int
+ 5,398,820 randoms generated [System.Random Integers] ~ 618 cycles/int
+ 4,758,575 randoms generated [System.Random Bools] ~ 701 cycles/int
+
+Finally, in revision a837e1ffb294234dc I tweaked the restricted
+Float/Double instances to use the new versions:
+
+ Next timing range-restricted System.Random.randomR:
+ 4,015,910 randoms generated [System.Random Ints] ~ 831 cycles/int
+ 7,572,249 randoms generated [System.Random Word16s] ~ 440 cycles/int
+ 12,768,688 randoms generated [System.Random Floats] ~ 261 cycles/int
+ 12,716,471 randoms generated [System.Random CFloats] ~ 262 cycles/int
+ 3,948,403 randoms generated [System.Random Doubles] ~ 845 cycles/int
+ 2,469,778 randoms generated [System.Random CDoubles] ~ 1,350 cycles/int
+ 4,542,423 randoms generated [System.Random Integers] ~ 734 cycles/int
+ 4,884,380 randoms generated [System.Random Bools] ~ 683 cycles/int
+
+Why would Floats be faster than Word16s though? Still some exploring left to do...
+
diff --git a/System/Random.hs b/System/Random.hs
index 0a6c82b..0acbf9c 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -308,27 +308,24 @@ instance Random Integer where
randomR ival g = randomIvalInteger ival g
random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
-#define TEMPTEST randomIvalBits
--- define TEMPTEST randomIvalIntegral
-
-instance Random Int where randomR = TEMPTEST; random = randomBits WORD_SIZE_IN_BITS
-instance Random Int8 where randomR = TEMPTEST; random = randomBits 8
-instance Random Int16 where randomR = TEMPTEST; random = randomBits 16
-instance Random Int32 where randomR = TEMPTEST; random = randomBits 32
-instance Random Int64 where randomR = TEMPTEST; random = randomBits 64
+instance Random Int where randomR = randomIvalBits; random = randomBits WORD_SIZE_IN_BITS
+instance Random Int8 where randomR = randomIvalBits; random = randomBits 8
+instance Random Int16 where randomR = randomIvalBits; random = randomBits 16
+instance Random Int32 where randomR = randomIvalBits; random = randomBits 32
+instance Random Int64 where randomR = randomIvalBits; random = randomBits 64
#ifndef __NHC__
-- Word is a type synonym in nhc98.
-instance Random Word where randomR = TEMPTEST; random = randomBounded
+instance Random Word where randomR = randomIvalBits; random = randomBounded
#endif
-instance Random Word8 where randomR = TEMPTEST; random = randomBits 8
-instance Random Word16 where randomR = TEMPTEST; random = randomBits 16
-instance Random Word32 where randomR = TEMPTEST; random = randomBits 32
-instance Random Word64 where randomR = TEMPTEST; random = randomBits 64
+instance Random Word8 where randomR = randomIvalBits; random = randomBits 8
+instance Random Word16 where randomR = randomIvalBits; random = randomBits 16
+instance Random Word32 where randomR = randomIvalBits; random = randomBits 32
+instance Random Word64 where randomR = randomIvalBits; random = randomBits 64
-instance Random CChar where randomR = TEMPTEST; random = randomBits 8
-instance Random CSChar where randomR = TEMPTEST; random = randomBits 8
-instance Random CUChar where randomR = TEMPTEST; random = randomBits 8
+instance Random CChar where randomR = randomIvalBits; random = randomBits 8
+instance Random CSChar where randomR = randomIvalBits; random = randomBits 8
+instance Random CUChar where randomR = randomIvalBits; random = randomBits 8
-- TODO: Finish applying randomBits after I double check all the sizes:
instance Random CShort where randomR = randomIvalIntegral; random = randomBounded
@@ -391,7 +388,6 @@ instance Random Double where
instance Random Float where
randomR = randomRFloating
random rng =
- -- TODO: Faster to just use 'next' IF it generates enough bits of randomness.
case rand of
(x,rng') ->
-- We use 24 bits of randomness corresponding to the 24 bit significand:
@@ -449,10 +445,9 @@ randomBits desired gen =
#endif
(acc `shiftL` c .|. shifted, g')
in loop gen 0 desired
- Nothing -> error "TODO: IMPLEMENT ME"
+ Nothing -> error "TODO: IMPLEMENT ME - handle undesirable bit sources"
where
-#if 1
--------------------------------------------------------------------------------
-- TEMP: These should probably be in Data.Bits AND they shoul have hardware support:
-- The number of leading zero bits:
@@ -507,22 +502,11 @@ randomIvalBits (l,h) rng
(x,g') | x >= cutoff -> rollAndTrash g'
(x,g') -> (x `mod` range, g')
--- Find the smallest power of two greater than or equal to the given number.
--- findBoundingPow2 :: (Bits a, Ord a) => a -> Int
--- findBoundingPow2 num | num <= 0 = error "findBoundingPow2 should not be given a non-positive number"
--- findBoundingPow2 num =
--- if num == bit (leadPos-1)
--- then leadPos-1
--- else leadPos
--- where
--- leadPos = bitSize num - bitScanReverse num
-
-- Find the smallest power of two greater than the given number.
-- Treat all numbers as unsigned irrespective of type:
findBoundingPow2 :: (Bits a, Ord a) => a -> Int
-- findBoundingPow2 num | num <= 0 = error "findBoundingPow2 should not be given a non-positive number"
findBoundingPow2 num = bitSize num - bitScanReverse num
-#endif
randomBounded :: (RandomGen g, Random a, Bounded a) => g -> (a, g)
More information about the ghc-commits
mailing list