Commit Graph

76 Commits

Author SHA1 Message Date
ringabout
0d53b6e027 fixes #23915; std/random produces different results on c/js (#24003)
fixes #23915
2024-08-25 22:23:30 +02:00
ringabout
4d11d0619d complete std prefixes for stdlib (#22887)
follow up https://github.com/nim-lang/Nim/pull/22851
follow up https://github.com/nim-lang/Nim/pull/22873
2023-10-30 17:03:04 +01:00
ringabout
f3a7622514 fixes #22360; compare with the half of randMax (#22361)
* fixes #22360; compare with the half of randMax

* add a test
2023-08-02 10:58:29 +02:00
metagn
f05387045d int64/uint64 as bigint in JS (#21613)
* int64/uint64 as bigint in JS

* fix CI

* convert to compile option

* fix lie

* smaller diff, changelog entry
2023-04-11 21:20:20 +02:00
metagn
739e1badb6 stdlib organization & documentation improvements (#20971)
* stdlib organization & documentation improvements

* fix CI

* Update doc/lib.md

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>

* fix ci, remove jshttpcore, export in jsfetch instead

* fix alphabetical order violations

* add cmdline, db_odbc

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
2022-12-06 09:37:03 +01:00
ringabout
e1ddd2d524 put std/threads under the umbrella of nimPreviewSlimSystem (#20711)
* put `std/threads` under the umbrella of `nimPreviewSlimSystem`

* add changelog

* fixes tests

* fixes tests again

* fixes tests
2022-10-31 06:36:50 +01:00
ringabout
3d2f0e2c7c make more standard libraries work with nimPreviewSlimSystem (#20343)
* make more standard libraries work with `nimPreviewSlimSystem`

* typo

* part two

* Delete specutils.nim

* fixes more tests

* more fixes

* fixes tests

* fixes three more tests

* add formatfloat import

* fix

* last
2022-09-27 20:06:23 +02:00
Antonis Geralis
a52918a994 Add missing rand(var Rand, Ordinal) overload (#20124)
* Add missing rand(var Rand, Ordinal) overload

* Corrected mistake, thanks @metagn

Co-authored-by: metagn <metagngn@gmail.com>

* Update random.nim

Co-authored-by: metagn <metagngn@gmail.com>
2022-08-26 15:39:12 -04:00
ringabout
59befed8ee prevent cache thrashing (#20129)
* prevent cache thrash

Co-authored-by: Charles Blake <cb@cblake.net>

* Update lib/pure/random.nim

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

Co-authored-by: Charles Blake <cb@cblake.net>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
2022-08-02 00:06:27 +08:00
ringabout
a8590d6707 [ORC] replace threadpool with createThread in the runnableExamples (#20106)
replace threadpool with createThreads in the runnableExamples
2022-07-31 20:02:23 -04:00
Antonis Geralis
f34734ffb4 Improve rand(bool) (#20045)
* Improve rand(bool)

* Use sign test instead of mod 2

* Use mod 2 again, as it works for js

* Use right shift as suggested by the authors of xoroshiro

* Update random.nim

* General case doesn't need any right shift it was correct to begin with

* Update random.nim

* add comment

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
2022-07-18 21:18:12 +02:00
Amjad Ben Hedhili
cf78c02b70 Make random.rand work with Ordinal (#20043)
* Make `random.rand` work with `Ordinal`

* Add changelog entry

* It's fine to cast to char
2022-07-16 16:51:27 -04:00
Tomohiro
7c8ea490a2 Fix initrand to avoid random number sequences overlapping (#18744)
* Fix initrand to avoid random number sequences overlapping

* Minor fix

* Fix compile error on js backend

* Disable new test for js backend

* Minor fix

* tempfiles module uses random.initRand()

* Remove unused module import from lib/std/tempfiles.nim

* Initialize baseState in initRand()

* Run tests/stdlib/trandom.nim from tests/test_nimscript.nims

* baseState is initialized only with sysrand.urandom and quit if failed

* Add comments
2021-09-02 14:12:14 +02:00
Timothee Cour
76f74fae88 std/random: fix overflow bugs; fixes #16360; fixes #16296; fixes #17670 (#18456) 2021-07-23 13:41:16 +02:00
Antonis Geralis
d1d2498c7b Add lent annotation to avoid extra copies in sample (#18404)
* Add lent annotation to avoid extra copies in sample

* Remove lent

* Update random.nim
2021-07-02 23:20:59 +02:00
Timothee Cour
45490497e3 fix #17467 1st call to rand is now non-skewed; allow seed == 0 (#17468)
* fix #17467 1st call to rand is now non-skewed; allow passing 0 as seed

* changelog + fallback

* document behavior for seed == 0

* address comments

* _

* fix tests, disable kdtree

* re-enable kdtree with -d:nimLegacyRandomInitRand
2021-05-11 11:04:59 +02:00
Andreas Rumpf
a9b62de895 CIs: attempt to use csources_v1 (#16282)
* CIs: attempt to use csources_v1
* also updated the BSDs
* also updated azure pipelines
* std modules should not itself use the 'std/' import dir...
* compiler has to be careful with std/ for v1 booting
2021-04-21 07:41:33 +02:00
Dominik Picheta
00f86f5591 Better error message for rand(153 .. 102) (#17031)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2021-02-15 08:56:17 -08:00
konsumlamm
f57774e1e7 Improve documentation for random (#17015)
* Improve documentation for random

Use runnableExamples
Minor changes

* Apply suggestions

Remove echo
Use RNG in more places

* Fix skipRandomNumbers example
2021-02-12 15:10:12 +01:00
flywind
18c24eb4d0 add system random to stdlib: std/sysrand (#16459) 2021-02-11 16:00:01 -08:00
hlaaftana
4fac8af0c9 Add initRand() with seed based on time (#16953) 2021-02-07 21:15:51 -08:00
Elliot Waite
f3d57761ab Fix broken links in docs (#16336)
* Fix broken links in docs

* Fix rand HSlice links
2020-12-14 20:13:12 +01:00
flywind
a32acc351e Revert "use generics in random (#16283)" (#16291)
This reverts commit 71e2a9e569.
2020-12-09 09:36:46 +08:00
flywind
71e2a9e569 use generics in random (#16283)
* use generics in random

* fix
2020-12-07 16:38:13 +01:00
flywind
cbc793b30b move tests to testament (#16101)
* move tests to testament

* minor

* fix random

* disable test random
2020-11-24 19:06:41 +01:00
Antonis Geralis
53d9902347 Make default state public (#15763)
* Make default state public

* Address review comments

Co-authored-by: b3liever <b3liever@yandex.com>
2020-10-29 11:14:22 +01:00
b3liever
59ba4d8c03 added normal variate function (#14725)
* added normal variate function
* change method only slightly faster
* changelog + since

Co-authored-by: b3liever <b3liever@yandex.com>
2020-06-22 20:48:33 +02:00
Miran
e7f280bd26 Remove deprecated stuff from stdlib (#14699)
* update to the latest Jester

* remove deprecated procs from some stdlib modules

* 'criterion' is not maintained anymore and relies on obsolete stuff
2020-06-17 15:25:02 +02:00
Jacek Sieka
7d6cbf290a Error -> Defect for defects (#13908)
* Error -> Defect for defects

The distinction between Error and Defect is subjective,
context-dependent and somewhat arbitrary, so when looking at an
exception, it's hard to guess what it is - this happens often when
looking at a `raises` list _without_ opening the corresponding
definition and digging through layers of inheritance.

With the help of a little consistency in naming, it's at least possible
to start disentangling the two error types and the standard lib can set
a good example here.
2020-04-28 19:56:01 +02:00
Miran
352232e62d style fix: change 'JS' to 'js' to make it consistent (#13168) 2020-01-16 14:14:03 +01:00
Miran
1551fff853 clean up deprecated stuff and unused imports in tests (#13059) 2020-01-07 09:49:08 +01:00
Judd
8b1ef8e07e allow random module to be used in standalone: (#12617)
proc randomized*() uses time releated function which is not available on
standalone, so disable this function for standalone.
2019-11-07 14:48:33 +01:00
Jjp137
93461aee34 Fix many broken links
Note that contrary to what docgen.rst currently says, the ids have
to match exactly or else most web browsers will not jump to the
intended symbol.
2019-10-22 17:59:12 -07:00
Miran
a5ab502f08 [backport] fix type's case in random.nim (#12445) 2019-10-17 22:13:00 +02:00
Miran
5f5ac8ce16 [backport] fix #12418, fix random.randomize on JS backend (#12432) 2019-10-15 16:29:32 +02:00
narimiran
6c994b2498 [backport] run nimpretty on numbers stuff 2019-09-30 13:58:05 +02:00
Jjp137
a906b3952b random: minor documentation updates (#11831)
Add docs for the sample procs that take in sets, and clean up a
few other minor things.
2019-08-02 02:42:18 -04:00
Miran
3bb6d1916e fix #11772, JS random seed was too large (#11820) 2019-07-25 04:53:50 -04:00
Miran
ac3449b93b [bugfix] fix #11450, random.initRand crashes in JS backend (#11522) 2019-06-17 20:46:04 +02:00
LemonBoy
7d217a71d3 Render deprecated pragmas (#8886)
* Render deprecated pragmas
* fix the expected html
* clean up the documentation regarding deprecations
* fix typo
* fix system.nim
* fix random
2019-06-03 10:15:20 +02:00
Mamy André-Ratsimbazafy
5363b8cc59 Reallow float ranges in random module 2019-05-08 13:48:03 +02:00
Jasper Jenkins
3d0190f470 Fix unexpected result of rand on a range, fixes #11015 (#11035) 2019-04-30 09:28:28 +02:00
Miran
9f94199d0c random: works for slices and enums, fixes #7698 (#10998) 2019-04-11 08:38:18 +02:00
WhiteDuke
1397ad7c54 [random] add support for sets (#10532)
* Support for sets

* Rename 'rand' to 'sample'

* Update random.nim
2019-02-25 16:45:44 +01:00
Timothee Cour
304b1dd34b randomize: added doAssert(seed!=0) to avoid invalid (non-random) behavior (#10635) 2019-02-13 23:30:14 +01:00
Jjp137
7bb94abc31 better docs: random (#10546) 2019-02-13 23:30:14 +01:00
Arne Döring
2b37bf5d5b added proc rand for typedesc (#10259) 2019-01-14 09:20:31 +01:00
c-blake
7ac1fc81fd Resolve things raised in https://github.com/nim-lang/Nim/issues/10081 ? (#10084)
* Resolve things raised in https://github.com/nim-lang/Nim/issues/10081 ?
CDF is a standard ident in all things related to random numbers/sampling,
and full words "cumulativeDistributionFunction" would be silly long, in
this case, IMO.  We use lowercase `cdf` to make it not look like a type,
remove all looping from `sample` letting callers do it.  Besides just
side-stepping any `sampleSize` name choice, callers may want to filter
out samples anyway which this makes slightly simpler.

Also add two variants of `cumsum`, value return and in-place update
distinguished by the var-ness of the first argument.  Add tests for
`int` and `float` for both `cumsum` and the new `sample`.  (The sample
tests exercise the value return mode of `cumsum`.)

Functionality pre-this-PR `sample(a, w)` is now the almost as simple
`for i in 0..<n: sample(a, w.cumsum)`, but this new code factoring is
almost surely better.  The statistical tests pass, as before.

* Address Araq comment in https://github.com/nim-lang/Nim/pull/10084
We can always add in some `var` version later if desired to save
memory, but this change now at least firms up the `sample` interface.

* Rename `cumsum` -> `cumsummed` to honor NEP1 style.  Re-instate `cumsum` as
the in-place transformation.  Test both in `tests/stdlib/tmath.nim` and use
`cumsummed` in the example code for sample since that's a simpler example.

* Fix requests from https://github.com/nim-lang/Nim/pull/10084 :
  example in lib/pure/math.nim and comment whitespace in lib/pure/random.nim
2018-12-31 14:52:51 +01:00
Dominik Picheta
513a287c61 Revert sub-second randomize(). Fixes randomize for JS backend. (#10000)
Fixes #9999.
2018-12-27 11:55:21 +01:00
c-blake
e1d5356ae9 Add ability to sample elements from openArray according to a weight array (#10072)
* Add the ability to sample elements from an openArray according to a parallel
array of weights/unnormalized probabilities (any sort of histogram, basically).
Also add a non-thread safe version for convenience.

* Address Araq comments on https://github.com/nim-lang/Nim/pull/10072

* import at top of file and space after '#'.

* Put in a check for non-zero total weight.

* Clarify constraint on `w`.

* Rename `rand(openArray[T])` to `sample(openArray[T])` to `sample`, deprecating
old name and name new (openArray[T], openArray[U]) variants `sample`.

* Rename caller-provided state version of rand(openArray[T]) and also clean
up doc comments.

* Add test for new non-uniform array sampler.  3 sd bound makes it 99% likely
that it will still pass in the future if the random number generator changes.
We cannot both have a tight bound to check distribution *and* loose check to
ensure resilience to RNG changes.  (We cannot *guarantee* resilience, anyway.
There's always a small chance any test hits a legitimate random fluctuation.)
2018-12-23 12:23:20 +00:00