Skip to content

Add "randInt" template function for random number#540

Merged
creativeprojects merged 1 commit intocreativeprojects:masterfrom
hansmi:rand1
Jul 30, 2025
Merged

Add "randInt" template function for random number#540
creativeprojects merged 1 commit intocreativeprojects:masterfrom
hansmi:rand1

Conversation

@hansmi
Copy link
Copy Markdown
Contributor

@hansmi hansmi commented Jul 28, 2025

The randInt function added by this change returns a pseudo-random number in a given range and using a seed for repeatable randomness. It can be used, for example, in cron schedules to pick a minute offset based on an environment variable (e.g. hostname).

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

Walkthrough

A new deterministic pseudo-random integer template function, randInt, has been implemented and exposed to templates. Documentation has been updated to describe its usage, and new tests have been added to verify its correctness across various input scenarios. No other functional or control flow changes are present.

Changes

Cohort / File(s) Change Summary
Documentation Update
docs/content/configuration/templates.md
Documents the new randInt template function, its parameters, deterministic behaviour, and example usage.
Template Function Implementation
util/templates/functions.go
Implements the randInt function using a seeded PCG random generator and exposes it in the template functions.
Template Function Tests
util/templates/functions_test.go
Adds tests for randInt covering different seeds and integer ranges, verifying deterministic output.

Sequence Diagram(s)

sequenceDiagram
    participant Template as Template Engine
    participant Funcs as TemplateFuncs Map
    participant Rand as randInt Function
    participant RNG as PCG Random Generator

    Template->>Funcs: Call "randInt" with (min, max, seed)
    Funcs->>Rand: Invoke randInt(min, max, seed)
    Rand->>Rand: Compute MD5(seed)
    Rand->>RNG: Seed PCG RNG with hash
    RNG->>Rand: Generate random int in [min, max)
    Rand->>Funcs: Return int64 result
    Funcs->>Template: Substitute value in template
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ab95380 and 93e7f2e.

📒 Files selected for processing (3)
  • docs/content/configuration/templates.md (1 hunks)
  • util/templates/functions.go (4 hunks)
  • util/templates/functions_test.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • docs/content/configuration/templates.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • util/templates/functions_test.go
  • util/templates/functions.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
util/templates/functions_test.go (1)

62-64: Enhance randInt edge-case coverage

The implementation always returns min when max = min + 1 (Int64N(1) ⇒ 0). It would be valuable to add at least one test for this single-value range:

• In util/templates/functions_test.go, immediately after the existing cases (around line 64), add:

{template: `{{ "" | randInt 5 6 }}`, expected: `5`}, // single-value range (max = min+1)

Optionally, to explicitly verify determinism, you could add a template that calls randInt twice with the same seed and asserts the two outputs are equal—if the test harness allows a boolean assertion.

Please confirm these expected values and test-harness support for repeat-call assertions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99e6d33 and 27a7612.

📒 Files selected for processing (3)
  • docs/content/configuration/templates.md (1 hunks)
  • util/templates/functions.go (4 hunks)
  • util/templates/functions_test.go (1 hunks)
🔇 Additional comments (3)
util/templates/functions.go (2)

4-4: LGTM! Import additions are appropriate.

The new imports (crypto/md5, encoding/binary, math/rand/v2) are correctly added and necessary for the randInt function implementation.

Also applies to: 6-6, 11-11


49-49: LGTM! Function registration and documentation are correct.

The randInt function is properly registered in the TemplateFuncs map and the documentation comment accurately describes the expected usage pattern.

Also applies to: 71-71

docs/content/configuration/templates.md (1)

603-605: Documentation is accurate and well-formatted.

The documentation correctly describes the randInt function behaviour, matches the implementation signature, and follows the established documentation pattern. The example clearly demonstrates the deterministic nature of the function.

Comment thread util/templates/functions.go
@creativeprojects
Copy link
Copy Markdown
Owner

Hey!

Thanks for this PR 👍🏻

I'm OK with using md5 here.
You'll need to prefix the line, or add at the end of the line this tag to tell the linter we know it's generally not recommended to use md5: //nolint:gosec

You can do the same for the min (and max?) or just rename them, like minValue or something 👍🏻

The `randInt` function added by this change returns a pseudo-random
number in a given range and using a seed for repeatable randomness. It
can be used, for example, in cron schedules to pick a minute offset
based on an environment variable (e.g. hostname).
@hansmi
Copy link
Copy Markdown
Contributor Author

hansmi commented Jul 29, 2025

@creativeprojects, thank you for the quick review. I added the nolint comments and renamed min/max to low/high.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 29, 2025

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.33%. Comparing base (99e6d33) to head (93e7f2e).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
util/templates/functions.go 81.82% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #540   +/-   ##
=======================================
  Coverage   79.33%   79.33%           
=======================================
  Files         136      136           
  Lines       13305    13316   +11     
=======================================
+ Hits        10555    10564    +9     
- Misses       2332     2333    +1     
- Partials      418      419    +1     
Flag Coverage Δ
unittests 79.33% <81.82%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@creativeprojects
Copy link
Copy Markdown
Owner

Looks good to me, thanks for this PR 👍🏻

@creativeprojects creativeprojects merged commit c7e87a1 into creativeprojects:master Jul 30, 2025
10 checks passed
@creativeprojects creativeprojects added this to the v0.32.0 milestone Jul 30, 2025
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.

2 participants