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

Safety of generating random numbers #529

Closed
chengxo opened this issue Jan 21, 2024 · 2 comments
Closed

Safety of generating random numbers #529

chengxo opened this issue Jan 21, 2024 · 2 comments

Comments

@chengxo
Copy link

chengxo commented Jan 21, 2024

Hi,

Is it safe to start a random number generator and using @turbo to vectorize the process of filling an array? like

using LoopVectorization, Random
rng = MersenneTwister(0)
a = zeros(100)
@turbo for i in eachindex(a)
a[i] = randn(rng)
end

I wonder whether it will cause redundance of entries. I can test on my computer, but I would like to know whether the behavior is guaranteed to be correct from its principle.
Thanks very much!

@chriselrod
Copy link
Member

It will cause redundancy of entries/will not be valid.
If you'd like a faster random number generator, you can try https://github.com/JuliaSIMD/VectorizedRNG.jl
I never optimized it for generating single numbers at a time, but randn! or rand! methods for filling arrays should be fast. It can also fuse multiplying and adding numbers for different ranges, e.g. randn!(local_rng(), dst, static(0), mu, sigma) should sample from a Normal(mu, sigma).

@chengxo
Copy link
Author

chengxo commented Jan 22, 2024

Thank you very much!

@chengxo chengxo closed this as completed Jan 22, 2024
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

No branches or pull requests

2 participants