moved random procs from math to its own module (breaking change)
This commit is contained in:
parent
5575cfd3ec
commit
95bb19a579
12 changed files with 43 additions and 122 deletions
|
|
@ -657,7 +657,7 @@ template newSeqWith*(len: int, init: expr): expr =
|
||||||
## seq2D[1][0] = true
|
## seq2D[1][0] = true
|
||||||
## seq2D[0][1] = true
|
## seq2D[0][1] = true
|
||||||
##
|
##
|
||||||
## import math
|
## import random
|
||||||
## var seqRand = newSeqWith(20, random(10))
|
## var seqRand = newSeqWith(20, random(10))
|
||||||
## echo seqRand
|
## echo seqRand
|
||||||
var result {.gensym.} = newSeq[type(init)](len)
|
var result {.gensym.} = newSeq[type(init)](len)
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,8 @@ proc advice*(s: var ThreadPoolState): ThreadPoolAdvice =
|
||||||
inc s.calls
|
inc s.calls
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
when not defined(testing) and isMainModule:
|
||||||
|
import random
|
||||||
|
|
||||||
proc busyLoop() =
|
proc busyLoop() =
|
||||||
while true:
|
while true:
|
||||||
discard random(80)
|
discard random(80)
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,8 @@
|
||||||
## constructor should be used for this purpose. However,
|
## constructor should be used for this purpose. However,
|
||||||
## currently only basic authentication is supported.
|
## currently only basic authentication is supported.
|
||||||
|
|
||||||
import net, strutils, uri, parseutils, strtabs, base64, os, mimetypes, math
|
import net, strutils, uri, parseutils, strtabs, base64, os, mimetypes,
|
||||||
|
math, random
|
||||||
import asyncnet, asyncdispatch
|
import asyncnet, asyncdispatch
|
||||||
import nativesockets
|
import nativesockets
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,6 @@ proc fac*(n: int): int {.noSideEffect.} =
|
||||||
|
|
||||||
when defined(Posix) and not defined(haiku):
|
when defined(Posix) and not defined(haiku):
|
||||||
{.passl: "-lm".}
|
{.passl: "-lm".}
|
||||||
when not defined(js) and not defined(nimscript):
|
|
||||||
import times
|
|
||||||
|
|
||||||
const
|
const
|
||||||
PI* = 3.1415926535897932384626433 ## the circle constant PI (Ludolph's number)
|
PI* = 3.1415926535897932384626433 ## the circle constant PI (Ludolph's number)
|
||||||
|
|
@ -119,30 +117,6 @@ proc sum*[T](x: openArray[T]): T {.noSideEffect.} =
|
||||||
## If `x` is empty, 0 is returned.
|
## If `x` is empty, 0 is returned.
|
||||||
for i in items(x): result = result + i
|
for i in items(x): result = result + i
|
||||||
|
|
||||||
proc random*(max: int): int {.benign.}
|
|
||||||
## Returns a random number in the range 0..max-1. 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.
|
|
||||||
|
|
||||||
proc random*(max: float): float {.benign.}
|
|
||||||
## 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 has a 16-bit resolution on windows
|
|
||||||
## and a 48-bit resolution on other platforms.
|
|
||||||
|
|
||||||
when not defined(nimscript):
|
|
||||||
proc randomize*() {.benign.}
|
|
||||||
## Initializes the random number generator with a "random"
|
|
||||||
## number, i.e. a tickcount. Note: Does nothing for the JavaScript target,
|
|
||||||
## as JavaScript does not support this. Nor does it work for NimScript.
|
|
||||||
|
|
||||||
proc randomize*(seed: int) {.benign.}
|
|
||||||
## Initializes the random number generator with a specific seed.
|
|
||||||
## Note: Does nothing for the JavaScript target,
|
|
||||||
## as JavaScript does not support this.
|
|
||||||
|
|
||||||
{.push noSideEffect.}
|
{.push noSideEffect.}
|
||||||
when not defined(JS):
|
when not defined(JS):
|
||||||
proc sqrt*(x: float32): float32 {.importc: "sqrtf", header: "<math.h>".}
|
proc sqrt*(x: float32): float32 {.importc: "sqrtf", header: "<math.h>".}
|
||||||
|
|
@ -239,57 +213,6 @@ when not defined(JS):
|
||||||
proc tgamma*(x: float64): float64 {.importc: "tgamma", header: "<math.h>".}
|
proc tgamma*(x: float64): float64 {.importc: "tgamma", header: "<math.h>".}
|
||||||
## The gamma function
|
## The gamma function
|
||||||
|
|
||||||
# C procs:
|
|
||||||
when defined(vcc) and false:
|
|
||||||
# The "secure" random, available from Windows XP
|
|
||||||
# https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
|
|
||||||
# Present in some variants of MinGW but not enough to justify
|
|
||||||
# `when defined(windows)` yet
|
|
||||||
proc rand_s(val: var cuint) {.importc: "rand_s", header: "<stdlib.h>".}
|
|
||||||
# To behave like the normal version
|
|
||||||
proc rand(): cuint = rand_s(result)
|
|
||||||
else:
|
|
||||||
proc srand(seed: cint) {.importc: "srand", header: "<stdlib.h>".}
|
|
||||||
proc rand(): cint {.importc: "rand", header: "<stdlib.h>".}
|
|
||||||
|
|
||||||
when not defined(windows):
|
|
||||||
proc srand48(seed: clong) {.importc: "srand48", header: "<stdlib.h>".}
|
|
||||||
proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
|
|
||||||
proc random(max: float): float =
|
|
||||||
result = drand48() * max
|
|
||||||
else:
|
|
||||||
when defined(vcc): # Windows with Visual C
|
|
||||||
proc random(max: float): float =
|
|
||||||
# we are hardcoding this because
|
|
||||||
# importc-ing macros is extremely problematic
|
|
||||||
# and because the value is publicly documented
|
|
||||||
# on MSDN and very unlikely to change
|
|
||||||
# See https://msdn.microsoft.com/en-us/library/296az74e.aspx
|
|
||||||
const rand_max = 4294967295 # UINT_MAX
|
|
||||||
result = (float(rand()) / float(rand_max)) * max
|
|
||||||
proc randomize() = discard
|
|
||||||
proc randomize(seed: int) = discard
|
|
||||||
else: # Windows with another compiler
|
|
||||||
proc random(max: float): float =
|
|
||||||
# we are hardcoding this because
|
|
||||||
# importc-ing 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
|
|
||||||
|
|
||||||
when not defined(vcc): # the above code for vcc uses `discard` instead
|
|
||||||
# this is either not Windows or is Windows without vcc
|
|
||||||
when not defined(nimscript):
|
|
||||||
proc randomize() =
|
|
||||||
randomize(cast[int](epochTime()))
|
|
||||||
proc randomize(seed: int) =
|
|
||||||
srand(cint(seed)) # rand_s doesn't use srand
|
|
||||||
when declared(srand48): srand48(seed)
|
|
||||||
|
|
||||||
proc random(max: int): int =
|
|
||||||
result = int(rand()) mod max
|
|
||||||
|
|
||||||
proc trunc*(x: float32): float32 {.importc: "truncf", header: "<math.h>".}
|
proc trunc*(x: float32): float32 {.importc: "truncf", header: "<math.h>".}
|
||||||
proc trunc*(x: float64): float64 {.importc: "trunc", header: "<math.h>".}
|
proc trunc*(x: float64): float64 {.importc: "trunc", header: "<math.h>".}
|
||||||
## Truncates `x` to the decimal point
|
## Truncates `x` to the decimal point
|
||||||
|
|
@ -319,17 +242,10 @@ when not defined(JS):
|
||||||
## echo fmod(-2.5, 0.3) ## -0.1
|
## echo fmod(-2.5, 0.3) ## -0.1
|
||||||
|
|
||||||
else:
|
else:
|
||||||
proc mathrandom(): float {.importc: "Math.random", nodecl.}
|
|
||||||
proc floor*(x: float32): float32 {.importc: "Math.floor", nodecl.}
|
proc floor*(x: float32): float32 {.importc: "Math.floor", nodecl.}
|
||||||
proc floor*(x: float64): float64 {.importc: "Math.floor", nodecl.}
|
proc floor*(x: float64): float64 {.importc: "Math.floor", nodecl.}
|
||||||
proc ceil*(x: float32): float32 {.importc: "Math.ceil", nodecl.}
|
proc ceil*(x: float32): float32 {.importc: "Math.ceil", nodecl.}
|
||||||
proc ceil*(x: float64): float64 {.importc: "Math.ceil", nodecl.}
|
proc ceil*(x: float64): float64 {.importc: "Math.ceil", nodecl.}
|
||||||
proc random(max: int): int =
|
|
||||||
result = int(floor(mathrandom() * float(max)))
|
|
||||||
proc random(max: float): float =
|
|
||||||
result = float(mathrandom() * float(max))
|
|
||||||
proc randomize() = discard
|
|
||||||
proc randomize(seed: int) = discard
|
|
||||||
|
|
||||||
proc sqrt*(x: float32): float32 {.importc: "Math.sqrt", nodecl.}
|
proc sqrt*(x: float32): float32 {.importc: "Math.sqrt", nodecl.}
|
||||||
proc sqrt*(x: float64): float64 {.importc: "Math.sqrt", nodecl.}
|
proc sqrt*(x: float64): float64 {.importc: "Math.sqrt", nodecl.}
|
||||||
|
|
@ -398,14 +314,6 @@ proc `mod`*[T: float32|float64](x, y: T): T =
|
||||||
## echo (4.0 mod -3.1) # -2.2
|
## echo (4.0 mod -3.1) # -2.2
|
||||||
result = if y == 0.0: x else: x - y * (x/y).floor
|
result = if y == 0.0: x else: x - y * (x/y).floor
|
||||||
|
|
||||||
proc random*[T](x: Slice[T]): T =
|
|
||||||
## For a slice `a .. b` returns a value in the range `a .. b-1`.
|
|
||||||
result = random(x.b - x.a) + x.a
|
|
||||||
|
|
||||||
proc random*[T](a: openArray[T]): T =
|
|
||||||
## returns a random element from the openarray `a`.
|
|
||||||
result = a[random(a.low..a.len)]
|
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
|
@ -440,24 +348,6 @@ proc lcm*[T](x, y: T): T =
|
||||||
x div gcd(x, y) * y
|
x div gcd(x, y) * y
|
||||||
|
|
||||||
when isMainModule and not defined(JS):
|
when isMainModule and not defined(JS):
|
||||||
proc gettime(dummy: ptr cint): cint {.importc: "time", header: "<time.h>".}
|
|
||||||
|
|
||||||
# Verifies random seed initialization.
|
|
||||||
let seed = gettime(nil)
|
|
||||||
randomize(seed)
|
|
||||||
const SIZE = 10
|
|
||||||
var buf : array[0..SIZE, int]
|
|
||||||
# Fill the buffer with random values
|
|
||||||
for i in 0..SIZE-1:
|
|
||||||
buf[i] = random(high(int))
|
|
||||||
# Check that the second random calls are the same for each position.
|
|
||||||
randomize(seed)
|
|
||||||
for i in 0..SIZE-1:
|
|
||||||
assert buf[i] == random(high(int)), "non deterministic random seeding"
|
|
||||||
|
|
||||||
when not defined(testing):
|
|
||||||
echo "random values equal after reseeding"
|
|
||||||
|
|
||||||
# Check for no side effect annotation
|
# Check for no side effect annotation
|
||||||
proc mySqrt(num: float): float {.noSideEffect.} =
|
proc mySqrt(num: float): float {.noSideEffect.} =
|
||||||
return sqrt(num)
|
return sqrt(num)
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,14 @@ proc random*(max: float): float {.benign.} =
|
||||||
let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
|
let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
|
||||||
result = (cast[float](u) - 1.0) * max
|
result = (cast[float](u) - 1.0) * max
|
||||||
|
|
||||||
|
proc random*[T](x: Slice[T]): T =
|
||||||
|
## For a slice `a .. b` returns a value in the range `a .. b-1`.
|
||||||
|
result = random(x.b - x.a) + x.a
|
||||||
|
|
||||||
|
proc random*[T](a: openArray[T]): T =
|
||||||
|
## returns a random element from the openarray `a`.
|
||||||
|
result = a[random(a.low..a.len)]
|
||||||
|
|
||||||
proc randomize*(seed: int) {.benign.} =
|
proc randomize*(seed: int) {.benign.} =
|
||||||
## Initializes the random number generator with a specific seed.
|
## Initializes the random number generator with a specific seed.
|
||||||
state.a0 = uint64(seed shr 16)
|
state.a0 = uint64(seed shr 16)
|
||||||
|
|
@ -84,3 +92,18 @@ when not defined(nimscript):
|
||||||
randomize(int times.getTime())
|
randomize(int times.getTime())
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
proc main =
|
||||||
|
var occur: array[1000, int]
|
||||||
|
|
||||||
|
var x = 8234
|
||||||
|
for i in 0..100_000:
|
||||||
|
x = random(len(occur)) # myrand(x)
|
||||||
|
inc occur[x]
|
||||||
|
for i, oc in occur:
|
||||||
|
if oc < 69:
|
||||||
|
doAssert false, "too few occurances of " & $i
|
||||||
|
elif oc > 130:
|
||||||
|
doAssert false, "too many occurances of " & $i
|
||||||
|
main()
|
||||||
|
|
|
||||||
|
|
@ -310,7 +310,7 @@ macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
##
|
##
|
||||||
## import math
|
## import math, random
|
||||||
## proc defectiveRobot() =
|
## proc defectiveRobot() =
|
||||||
## randomize()
|
## randomize()
|
||||||
## case random(1..4)
|
## case random(1..4)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ discard """
|
||||||
action: run
|
action: run
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math, strutils
|
import math, random, strutils
|
||||||
const consolePrefix = "jsCallbacks"
|
const consolePrefix = "jsCallbacks"
|
||||||
|
|
||||||
asm """
|
asm """
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ discard """
|
||||||
output: "Success"
|
output: "Success"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math, threadPool
|
import math, random, threadPool
|
||||||
|
|
||||||
# ---
|
# ---
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import math
|
import math, random
|
||||||
import unittest
|
import unittest
|
||||||
import sets
|
import sets
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ test "unittest multiple requires":
|
||||||
require(true)
|
require(true)
|
||||||
|
|
||||||
|
|
||||||
import math
|
import math, random
|
||||||
from strutils import parseInt
|
from strutils import parseInt
|
||||||
proc defectiveRobot() =
|
proc defectiveRobot() =
|
||||||
randomize()
|
randomize()
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ discard """
|
||||||
|
|
||||||
# bug #1816
|
# bug #1816
|
||||||
|
|
||||||
from math import random
|
from random import random
|
||||||
from os import sleep
|
from os import sleep
|
||||||
|
|
||||||
type PComm = ptr Channel[int]
|
type PComm = ptr Channel[int]
|
||||||
|
|
|
||||||
13
web/news.txt
13
web/news.txt
|
|
@ -2,7 +2,7 @@
|
||||||
News
|
News
|
||||||
====
|
====
|
||||||
|
|
||||||
2016-XX-XX Version 0.13.1 released
|
2016-XX-XX Version 0.14.0 released
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
Changes affecting backwards compatibility
|
Changes affecting backwards compatibility
|
||||||
|
|
@ -39,6 +39,9 @@ Changes affecting backwards compatibility
|
||||||
you need that.
|
you need that.
|
||||||
- The ``json.%`` operator is now overloaded for ``object``, ``ref object`` and
|
- The ``json.%`` operator is now overloaded for ``object``, ``ref object`` and
|
||||||
``openarray[T]``.
|
``openarray[T]``.
|
||||||
|
- The procs related to ``random`` number generation in ``math.nim`` have
|
||||||
|
been moved to its own ``random`` module and been reimplemented in pure
|
||||||
|
Nim.
|
||||||
|
|
||||||
|
|
||||||
Library Additions
|
Library Additions
|
||||||
|
|
@ -64,11 +67,13 @@ Language Additions
|
||||||
|
|
||||||
- Nim now supports a ``.this`` pragma for more notational convenience.
|
- Nim now supports a ``.this`` pragma for more notational convenience.
|
||||||
- Nim now supports a different ``using`` statement for more convenience.
|
- Nim now supports a different ``using`` statement for more convenience.
|
||||||
- Nim now supports ``partial`` object declarations to mitigate the problems
|
|
||||||
that arise when types are mutually dependent and yet should be kept in
|
|
||||||
different modules.
|
|
||||||
- ``include`` statements are not restricted to top level statements anymore.
|
- ``include`` statements are not restricted to top level statements anymore.
|
||||||
|
|
||||||
|
..
|
||||||
|
- Nim now supports ``partial`` object declarations to mitigate the problems
|
||||||
|
that arise when types are mutually dependent and yet should be kept in
|
||||||
|
different modules.
|
||||||
|
|
||||||
|
|
||||||
2016-01-27 Nim in Action is now available!
|
2016-01-27 Nim in Action is now available!
|
||||||
==========================================
|
==========================================
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue