Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set-random-seed added #848

Merged
merged 13 commits into from
Feb 19, 2025
Merged

set-random-seed added #848

merged 13 commits into from
Feb 19, 2025

Conversation

DaddyWesker
Copy link
Contributor

@DaddyWesker DaddyWesker commented Feb 10, 2025

So, I've figured out how to properly use thread_local for creating thread_local global variable in Rust. Usage of random-int and random-float is the same. Base seed is coming from StdRng::from_os_rng(). New set-random-seed is used to set a random seed. So when this operation is called, GLOBAL_RNG (which is thread local) will be reinitialized with a new seed.

I'm not sure what kind of tests should I provide for set-random-seed operation so I've created kinda simple one.

Example is quite simple:
!(random-int 0 5) # just a regular call without pre-defined seed as before
!(set-random-seed! 5) # setting a seed to a random generator
!(random-int 0 5) # now calling random-int after reinitializing generator with a seed.

Update. It was decided to refactor API and add some additional functions like new-random-generator etc. So API is know looks like this:

  1. new-random-generator creates random generator instance for further usage. Takes seed as an argument;
  2. random-int\random-float now takes not only range but also instance of random generator;
  3. there is a metta-level (token) random generator with default behavior, e.g. StdRng::from_os_rng(); which called &rng and which can be used just like user-creeated random generator instance;
  4. set-random-seed accepts number and random generator instance and changes its behavior using random seed;
  5. reset-random-generator discards all changes to random generator instance and reinitialize it using StdRng::from_os_rng();.

Example of usage

!(bind! &newrng (new-random-generator 9)) # 9 is a seed for creating random generator instance
!(random-int &newrng 0 10) # using just created &newrng to generate a number
!(random-float &rng 0 10) # &rng is a global metta-level token
!(set-random-seed &newrng 5) # re-seeding random number generator instance
!(set-random-seed &rng 5) # we can also re-seed global &rng
!(reset-random-generator &rng) # resetting &rng to the default behavior

Copy link
Collaborator

@vsbogd vsbogd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test which checks that set-random-seed leads to deterministic results. Also I would add a code proposed by @Necr0x0Der if you have one.

vsbogd
vsbogd previously approved these changes Feb 10, 2025
Necr0x0Der
Necr0x0Der previously approved these changes Feb 10, 2025
@DaddyWesker DaddyWesker dismissed stale reviews from Necr0x0Der and vsbogd via 9647227 February 18, 2025 11:22
DaddyWesker and others added 5 commits February 18, 2025 14:24
Return sudden remove
This makes code simpler and isolates internal API from rand library
internals.
Wrap StdRng into RandomGenerator grounded atom
@Necr0x0Der Necr0x0Der merged commit d1e0d22 into trueagi-io:main Feb 19, 2025
1 check passed
@DaddyWesker DaddyWesker deleted the random_seed branch February 26, 2025 05:42
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.

3 participants