Skip to content

Fix #3: Add random number generator#31

Open
leftibot wants to merge 3 commits intoChaiScript:masterfrom
leftibot:fix/issue-3-add-random-number-generator
Open

Fix #3: Add random number generator#31
leftibot wants to merge 3 commits intoChaiScript:masterfrom
leftibot:fix/issue-3-add-random-number-generator

Conversation

@leftibot
Copy link
Copy Markdown

Automated fix by @leftibot.

What changed

Fix #3: Add random number generator module
Add chaiscript::extras::random providing rand(), rand(max), rand(min, max),
and srand(seed) functions using C++11's facilities (std::mt19937
with std::uniform_int_distribution). The engine state is shared via
std::shared_ptr so srand() produces deterministic sequences. Also add
CATCH_CONFIG_NO_POSIX_SIGNALS to test files to fix SIGSTKSZ build errors
on newer glibc.
Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

Files

 include/chaiscript/extras/random.hpp | 41 ++++++++++++++++++++++++++++++
 tests/CMakeLists.txt                 |  6 +++++
 tests/math.cpp                       |  1 +
 tests/random.cpp                     | 48 ++++++++++++++++++++++++++++++++++++
 tests/string_methods.cpp             |  1 +
 5 files changed, 97 insertions(+)

Closes #3

Triggered by @lefticus.

Add chaiscript::extras::random providing rand(), rand(max), rand(min, max),
and srand(seed) functions using C++11's <random> facilities (std::mt19937
with std::uniform_int_distribution). The engine state is shared via
std::shared_ptr so srand() produces deterministic sequences. Also add
CATCH_CONFIG_NO_POSIX_SIGNALS to test files to fix SIGSTKSZ build errors
on newer glibc.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@lefticus
Copy link
Copy Markdown
Member

@leftibot make this more like the C++ interface, where the user has to have the random number generator engine, and they can copy / seed it / etc, and then ask for values from it from a range, etc.

…rface

Replace free functions (rand/srand) with a Random_Engine class that wraps
std::mt19937. Users construct engine objects, copy them, seed them, and
generate values via random_int(min, max) and random_float(min, max).

Requested by @lefticus in PR ChaiScript#31 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leftibot
Copy link
Copy Markdown
Author

Addressed review feedback from @lefticus. New commits:

97843be Address review: expose Random_Engine type mirroring C++ <random> interface

@lefticus
Copy link
Copy Markdown
Member

@leftibot make it clear what random device is being used in the name of the random device.

Make the underlying random device (std::mt19937) explicit in the
type name, so users know which engine they are working with.

Requested by @lefticus in PR ChaiScript#31 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leftibot
Copy link
Copy Markdown
Author

Addressed review feedback from @lefticus. New commits:

955b4ba Address review: rename Random_Engine to MT19937_Engine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add random number generator

2 participants