set the random seed in julia generator of random numbers

不羁岁月 提交于 2021-01-21 06:10:08

问题


I would like to do a couple of checkings using the random generator for normal distributed numbers in julia. So what I would like is to obtain the same sequence of pseudo-random numbers.

Actually, I do random matrices, so I would like that both of my programs generate:

A = randn(dim,dim)                                                                                                                                                                           
H = (A + A')/sqrt(2)

the same H-matrix


回答1:


Updated answer, for Julia 0.7 onwards.

import Random
Random.seed!(1234)
dim = 5
A = randn(dim,dim)
H = (A + A')/sqrt(2)

Previous answer, for Julia 0.6 and earlier.

You are looking for the srand function, e.g.

srand(1234)
dim = 5
A = randn(dim,dim)
H = (A + A')/sqrt(2)

Will always produce the same results.




回答2:


In Julia 0.7/1.0, you can use Random.seed!(1234); https://docs.julialang.org/en/v1/stdlib/Random/index.html#Generators-(creation-and-seeding)-1



来源:https://stackoverflow.com/questions/25006370/set-the-random-seed-in-julia-generator-of-random-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!