Merge pull request #1206 from barcharcraz/windowsRandom
Windows random(float): float
This commit is contained in:
commit
2fa739a2e8
1 changed files with 14 additions and 7 deletions
|
|
@ -135,12 +135,12 @@ proc random*(max: int): int {.gcsafe.}
|
||||||
## which initializes the random number generator with a "random"
|
## which initializes the random number generator with a "random"
|
||||||
## number, i.e. a tickcount.
|
## number, i.e. a tickcount.
|
||||||
|
|
||||||
when not defined(windows):
|
proc random*(max: float): float {.gcsafe.}
|
||||||
proc random*(max: float): float {.gcsafe.}
|
|
||||||
## returns a random number in the range 0..<max. The sequence of
|
## returns a random number in the range 0..<max. The sequence of
|
||||||
## random number is always the same, unless `randomize` is called
|
## random number is always the same, unless `randomize` is called
|
||||||
## which initializes the random number generator with a "random"
|
## which initializes the random number generator with a "random"
|
||||||
## number, i.e. a tickcount. This is currently not supported for windows.
|
## number, i.e. a tickcount. This has a 16-bit resolution on windows
|
||||||
|
## and a 48-bit resolution on other platforms.
|
||||||
|
|
||||||
proc randomize*() {.gcsafe.}
|
proc randomize*() {.gcsafe.}
|
||||||
## initializes the random number generator with a "random"
|
## initializes the random number generator with a "random"
|
||||||
|
|
@ -205,7 +205,14 @@ when not defined(JS):
|
||||||
proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
|
proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
|
||||||
proc random(max: float): float =
|
proc random(max: float): float =
|
||||||
result = drand48() * max
|
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() =
|
proc randomize() =
|
||||||
randomize(cast[int](epochTime()))
|
randomize(cast[int](epochTime()))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue