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

Add seed control. #17

Open
Myridium opened this issue Mar 13, 2023 · 2 comments
Open

Add seed control. #17

Myridium opened this issue Mar 13, 2023 · 2 comments
Labels
enhancement New feature or request fix implemented A fix has been implemented, the issue will soon be resolved

Comments

@Myridium
Copy link

Is your feature request related to a problem? Please describe.
It's not possible for me to produce consistent output because there's no option to include a seed.
My use-case is hashing serializable objects, and using their hash as a random seed to give them a human-readable alias which is generated using wonderwords.

Describe the solution you'd like
I'd like an option to set the seed/random state in wonderwords.

Describe alternatives you've considered
It appears that using Python's random.seed(...) affects the random state of wonderwords. However it isn't enough. It does work during a Python session (i.e. it produces consistent results), but not across different sessions.

For example, consider this function:

import random
import wonderwords

def random_noun(seed : int):
    rstate = random.getstate()
    random.seed(seed)
    noun      = wonderwords.RandomWord().word(include_parts_of_speech=["nouns"])
    random.setstate(rstate)
    return noun

This allows me to produce consistent output when using random_noun(seed) during a program. It's a 'pure function', in the sense that it gives the same output for the same input, with no (known) side effects. However, if I quit Python and run a program again, the results of random_noun(seed) are changed. This means I cannot reproduce behaviour across different runs of the program.

@mrmaxguns
Copy link
Owner

I didn't even think about that use-case. I think the issue might be that internally in some places Wonderwords uses sets which have no guaranteed ordering. Setting the seed is definitely a valid concern, so I am looking for ways around that

@mrmaxguns mrmaxguns added the enhancement New feature or request label Apr 12, 2024
@mrmaxguns mrmaxguns pinned this issue Apr 12, 2024
@mrmaxguns mrmaxguns added the fix implemented A fix has been implemented, the issue will soon be resolved label Apr 12, 2024
@mrmaxguns
Copy link
Owner

I implemented a fix where the filter function always returns a sorted list of words. That makes word deterministic if the seed is specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix implemented A fix has been implemented, the issue will soon be resolved
Projects
None yet
Development

No branches or pull requests

2 participants