summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorCharlie <bartoc@umich.edu>2014-05-18 15:13:37 -0400
committerCharlie <bartoc@umich.edu>2014-05-18 15:13:37 -0400
commit8a183dac78a5076d421dcbff263c6d163fe2a7fc (patch)
treee8fda0958ff91878c04fb9c2ac113d3c467cd225 /lib
parente54ab22bf9a67353e5a70f56e7801624d68ca4f5 (diff)
downloadNim-8a183dac78a5076d421dcbff263c6d163fe2a7fc.tar.gz
added random(max: float): float support to windows
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/math.nim20
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index e4aecd272..24a2ec7fa 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -135,12 +135,11 @@ proc random*(max: int): int {.gcsafe.}
   ## which initializes the random number generator with a "random"
   ## number, i.e. a tickcount.
 
-when not defined(windows):
-  proc random*(max: float): float {.gcsafe.}
-    ## returns a random number in the range 0..<max. The sequence of
-    ## random number is always the same, unless `randomize` is called
-    ## which initializes the random number generator with a "random"
-    ## number, i.e. a tickcount. This is currently not supported for windows.
+proc random*(max: float): float {.gcsafe.}
+  ## returns a random number in the range 0..<max. The sequence of
+  ## random number is always the same, unless `randomize` is called
+  ## which initializes the random number generator with a "random"
+  ## number, i.e. a tickcount. This is currently not supported for windows.
 
 proc randomize*() {.gcsafe.}
   ## initializes the random number generator with a "random"
@@ -205,7 +204,14 @@ when not defined(JS):
     proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
     proc random(max: float): float =
       result = drand48() * max
-    
+  when defined(windows):
+    proc random(max: float): float =
+      # we are hardcodeing this because
+      # importcing macros is extremely problematic
+      # and because the value is publicly documented
+      # on MSDN and very unlikely to change
+      const rand_max = 32767
+      result = (float(rand()) / float(rand_max)) * max
   proc randomize() =
     randomize(cast[int](epochTime()))