Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

V2 is not an instance of Uniform or UniformRange. #182

Closed
vladov3000 opened this issue Jan 22, 2025 · 2 comments · Fixed by #183
Closed

V2 is not an instance of Uniform or UniformRange. #182

vladov3000 opened this issue Jan 22, 2025 · 2 comments · Fixed by #183

Comments

@vladov3000
Copy link
Contributor

I am trying to generate a random point in a rectangle. I would like to write the the following code:

randomPoint :: UniformRange a => V2 a -> V2 a -> V2 a
randomPoint topLeft bottomRight = uniformRM (topLeft, bottomRight) globalStdGen

However, Linear.V2 is not an instance of UniformRange. It implements Random, which only exists for backwards compatibility reasons:

Random exists primarily for backwards compatibility with version 1.1 of this library. In new code, use the better specified Uniform and UniformRange instead.

I can add Uniform and UniformRange instances for V2 and the other vector types myself if you would like.

Links:
Random
Uniform
UniformRange

@RyanGlScott
Copy link
Collaborator

I can add Uniform and UniformRange instances for V2 and the other vector types myself if you would like.

That would be a welcome change. Thanks!

@vladov3000
Copy link
Contributor Author

It is done. I added instances to all instances of Random in the project. The default implementations use Generic to fill out the definition automatically.

$ cabal repl
ghci> import Linear.V2
ghci> import System.Random.Stateful
ghci> uniformRM ((V2 1 2), (V2 3 4)) globalStdGen :: IO (V2 Int)
V2 1 3
ghci> uniformRM ((V2 1 2), (V2 3 4)) globalStdGen :: IO (V2 Int)
V2 2 3
ghci> uniformRM ((V2 1 2), (V2 3 4)) globalStdGen :: IO (V2 Int)
V2 2 3
ghci> uniformRM ((V2 1 2), (V2 3 4)) globalStdGen :: IO (V2 Int)
V2 2 4

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants