You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds cxxrandom unit test and fixes interface problems (#2099)
* Adds cxxrandom unit test and fixes interface problems
* Tightens braces
* Adds detection code for Shuffle's seqID/engID
* Adds usage examples for cxxrandom
* Gives cxxrandom objects id ranges, sort of
* Updates changelog
* Updates changelog.txt
* Increases id space for cxxrandom
* Fixes bool distribution error message and improves check
* Adds comment explaining the seeded RNG tests for cxxrandom
* Fixes type problem for 32bit builds
* Reduces loop count a few magnitudes
* Fixes a mistake in test.cxxrandom_seed
Copy file name to clipboardExpand all lines: docs/Lua API.rst
+37-2Lines changed: 37 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4401,7 +4401,7 @@ Native functions (exported to Lua)
4401
4401
4402
4402
adds a number to the sequence
4403
4403
4404
-
- ``ShuffleSequence(rngID, seqID)``
4404
+
- ``ShuffleSequence(seqID, rngID)``
4405
4405
4406
4406
shuffles the number sequence
4407
4407
@@ -4464,7 +4464,7 @@ Lua plugin classes
4464
4464
``bool_distribution``
4465
4465
~~~~~~~~~~~~~~~~~~~~~
4466
4466
4467
-
- ``init(min, max)``: constructor
4467
+
- ``init(chance)``: constructor
4468
4468
- ``next(id)``: returns next boolean in the distribution
4469
4469
4470
4470
- ``id``: engine ID to pass to native function
@@ -4477,6 +4477,41 @@ Lua plugin classes
4477
4477
- ``shuffle()``: shuffles the sequence of numbers
4478
4478
- ``next()``: returns next number in the sequence
4479
4479
4480
+
Usage
4481
+
-----
4482
+
4483
+
The basic idea is you create a number distribution which you generate random numbers along. The C++ relies
4484
+
on engines keeping state information to determine the next number along the distribution.
4485
+
You're welcome to try and (ab)use this knowledge for your RNG purposes.
4486
+
4487
+
Example::
4488
+
4489
+
local rng = require('plugins.cxxrandom')
4490
+
local norm_dist = rng.normal_distribution(6820,116) // avg, stddev
4491
+
local engID = rng.MakeNewEngine(0)
4492
+
-- somewhat reminiscent of the C++ syntax
4493
+
print(norm_dist:next(engID))
4494
+
4495
+
-- a bit more streamlined
4496
+
local cleanup = true --delete engine on cleanup
4497
+
local number_generator = rng.crng:new(engID, cleanup, norm_dist)
4498
+
print(number_generator:next())
4499
+
4500
+
-- simplified
4501
+
print(rng.rollNormal(engID,6820,116))
4502
+
4503
+
The number sequences are much simpler. They're intended for where you need to randomly generate an index, perhaps in a loop for an array. You technically don't need an engine to use it, if you don't mind never shuffling.
4504
+
4505
+
Example::
4506
+
4507
+
local rng = require('plugins.cxxrandom')
4508
+
local g = rng.crng:new(rng.MakeNewEngine(0), true, rng.num_sequence:new(0,table_size))
Copy file name to clipboardExpand all lines: docs/changelog.txt
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
42
42
- `tweak` partial-items: displays percentages on partially-consumed items such as hospital cloth
43
43
44
44
## Fixes
45
+
- `cxxrandom`: fixed exception when calling ``bool_distribution``
46
+
- `cxxrandom`: fixed id order for ShuffleSequence, but adds code to detect which parameter is which so each id is used correctly. 16000 limit before things get weird (previous was 16 bits)
45
47
- `autofarm` removed restriction on only planting 'discovered' plants
46
48
- `luasocket` (and others): return correct status code when closing socket connections
47
49
@@ -64,6 +66,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
64
66
- Include recently-added tweaks in example dfhack.init file, clean up dreamfort onMapLoad.init file
65
67
66
68
## Documentation
69
+
- `cxxrandom`: added usage examples
67
70
- Add more examples to the plugin skeleton files so they are more informative for a newbie
0 commit comments