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

Port math/rand to gno #1272

Closed
dragosroua opened this issue Oct 21, 2023 · 1 comment · Fixed by #2455
Closed

Port math/rand to gno #1272

dragosroua opened this issue Oct 21, 2023 · 1 comment · Fixed by #2455

Comments

@dragosroua
Copy link

Description

Following the steps described here.

@irreverentsimplicity
Copy link
Contributor

there is a working implementation of the math/rand, but it's a bit hacky. I'm using this in flippando, with relatively good results.

I'm pasting the relevant code for review here (you can ignore it, use it as a starting point in your own approach, or even ask me to do a PR, if you think it will cut it, for now).

this is located in p/math_rand/rand.gno in this repo https://github.com/irreverentsimplicity/gno/tree/feat/flippando

line 394 onward:

func (r *Rand) NormFloat64() float64 {
	z := r.Float64()*2 - 1 + r.Float64()*2 - 1
	radius := z*z
	for radius >= 1 || radius == 0 {
		z = r.Float64()*2 - 1 + r.Float64()*2 - 1
		radius = z*z
	}
	return z * (-2 * ln(radius) / radius)
}

// while not having access to math package at all
func ln(x float64) float64 {
    const negativeInfinity = -1e308
    const NaN = -99999.0

    if x == 0 {
        return negativeInfinity // Return a very large negative value for ln(0)
    }
    if x < 0 {
        return NaN // Return a special value for negative values
    }

    // Adjust large values into a suitable range for Maclaurin series
    if x > 2 {
        return -ln(1/x)
    }

    // Maclaurin series for ln(1 + x) around 0
    x -= 1 // Adjust x for the expansion around 1
    result := 0.0
    term := x
    for i := 1; i < 100; i++ { // Use 100 terms. You can adjust this for more accuracy.
        if i%2 == 1 {
            result += term / float64(i)
        } else {
            result -= term / float64(i)
        }
        term *= x
    }

    return result
}

thehowl added a commit that referenced this issue Jun 28, 2024
This PR ports over to Gno the
[math/rand/v2](https://pkg.go.dev/math/rand/v2) Go package. It provides
pseudo-random number generation.

Notable omissions:

- the [`N` generic function](https://pkg.go.dev/math/rand/[email protected]#N)
- The [chacha8 random
source](https://pkg.go.dev/math/rand/[email protected]#ChaCha8) (we can use
PCG instead; and chacha8 can be implemented later)

This PR requires a version bump to go 1.22 to have math/rand/v2 in the
standard libraries; otherwise the Native tests won't work. We can
rollback the changes to move it to 1.22 by removing the native math/rand
implementation, and only using the stdlibs.

BREAKING CHANGE: existing usages of math/rand used Go's math/rand;
however, in this new standard library we use its v2, so results will be
different.

Closes #1272; See-also #1267 

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
gfanton pushed a commit to gfanton/gno that referenced this issue Jul 23, 2024
This PR ports over to Gno the
[math/rand/v2](https://pkg.go.dev/math/rand/v2) Go package. It provides
pseudo-random number generation.

Notable omissions:

- the [`N` generic function](https://pkg.go.dev/math/rand/[email protected]#N)
- The [chacha8 random
source](https://pkg.go.dev/math/rand/[email protected]#ChaCha8) (we can use
PCG instead; and chacha8 can be implemented later)

This PR requires a version bump to go 1.22 to have math/rand/v2 in the
standard libraries; otherwise the Native tests won't work. We can
rollback the changes to move it to 1.22 by removing the native math/rand
implementation, and only using the stdlibs.

BREAKING CHANGE: existing usages of math/rand used Go's math/rand;
however, in this new standard library we use its v2, so results will be
different.

Closes gnolang#1272; See-also gnolang#1267 

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🌟 Wanted for Launch
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants