Skip to content

strings: add lorum Markov chain text generator #26387

Merged
spytheman merged 4 commits into
vlang:masterfrom
mike-ward:lorum
Jan 19, 2026
Merged

strings: add lorum Markov chain text generator #26387
spytheman merged 4 commits into
vlang:masterfrom
mike-ward:lorum

Conversation

@mike-ward

Copy link
Copy Markdown
Contributor

This PR integrates lorum into vlib/strings, refining the implementation to be suitable for the standard library.

Changes:

  • Removes rand dependency: Implements a simple internal LCG (Linear Congruential Generator) to handle pseudo-randomness. This prevents circular dependencies (e.g., strings -> rand -> time), which is critical for vlib core modules.
  • Deterministic by default: The generator now defaults to a fixed seed if none is provided, ensuring reproducible output for tests and simple calls.
  • Renames API: Renames lorem_generate() to lorum() for consistency with the file name and conciseness.
  • Updates Example: Refactors examples/lorem.v to use the new strings.lorum() API. The example now handles random seeding explicitly using rand and time to maintain the expected CLI behavior (random output by default).

Verification:

  • v test vlib/strings/lorum_test.v passes.
  • v run examples/lorem.v runs successfully and produces varied output.

Comment thread vlib/strings/lorem/lorem.v
@JalonSolov

Copy link
Copy Markdown
Collaborator

Just curious... why change lorem to lorum ?

@mike-ward

Copy link
Copy Markdown
Contributor Author

Just curious... why change lorem to lorum ?

That was a goof. Fixed it up in the most recent commit.

Comment thread examples/lorem.v
Comment on lines +43 to +45
t := time.now().unix_milli()
rand.seed([u32(t), u32(t >> 32)])
rng_seed = rand.int()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
t := time.now().unix_milli()
rand.seed([u32(t), u32(t >> 32)])
rng_seed = rand.int()
rng_seed = rand.int()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the default random number generator is already time seeded by default

@@ -0,0 +1,66 @@
module lorem

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
module lorem
import strings.lorem

@@ -0,0 +1,66 @@
module lorem

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
module lorem
import strings.lorem

Turning this into a normal (non-internal) test, has the benefit that all of the tests also become usage examples, and as a user, you can also more easily see, what the public (and well tested) APIs are.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

afaik only lorem_vary and lorem_tokenize are tested here, and are private?

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.
@mike-ward thank you 🙇🏻 .

@spytheman spytheman merged commit 7677d76 into vlang:master Jan 19, 2026
83 checks passed
@mike-ward

Copy link
Copy Markdown
Contributor Author

Thanks guys. I thought I had done my due diligence to make this easy to merge. I'll do better next time...

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