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 math.Rand that calls rand.Float64 #11833

Closed
godalming123 opened this issue Dec 26, 2023 · 3 comments · Fixed by #11838
Closed

Add math.Rand that calls rand.Float64 #11833

godalming123 opened this issue Dec 26, 2023 · 3 comments · Fixed by #11838

Comments

@godalming123
Copy link

Currently random ints have to be created like so:

{{ index (shuffle (seq 0 9)) 0 }}

Where 0 and 9 specify the range for the number to be generate in.

And random floats have to be generated like so:

{{ mul 0.1 (index (shuffle (seq 0 90)) 0) }}

Where 0.1 is the amount of precision desired, 0*the precision is the minimum bound, and 90*the precision is the upper bound.

It would be much easier if their were functions:

{{ randInt 0 9 }}
{{ randFloat 0.1 0 9 }} // Note that this will generate a number between 0 and 9 without having to change the bounds to adjust for the precision desired
@jmooring
Copy link
Member

Out of curiosity, how do you currently use the random numbers generated with your examples above?

I suspect we'd be more likely to implement a single math.Rand function using:
https://pkg.go.dev/math/rand#Float64

{{ math.Rand | mul 10 | add 1 | int }} --> random int between 1 and 10 inclusive

@godalming123
Copy link
Author

I'm shifting images that are placed on top of each other slightly so that all of them are visible. This requires generating a random float that is between 2 numbers.

@bep bep changed the title Create a random number function Add math.Rand that calls rand.Float64 Dec 28, 2023
@bep bep added this to the v0.122.0 milestone Dec 28, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 28, 2023
jmooring added a commit to jmooring/hugo that referenced this issue Dec 28, 2023
bep pushed a commit that referenced this issue Dec 29, 2023
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants