# Rand in Julia vs MATLAB

**URL:** <https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 17, 2017, 1:15pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505 "2017-10-17T13:15:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohith14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohith14/32/2229_2.png) [@rohith14](https://discourse.julialang.org/u/rohith14)\
**Post date:** [October 17, 2017, 1:15pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/1 "2017-10-17T13:15:24Z")

</div>

Hi,

Based on this issue, [https://github.com/JuliaLang/julia/issues/8606](https://github.com/JuliaLang/julia/issues/8606), I see we are generating different random number sequence in Julia and MATLAB. Is their a way to get same random numbers in both languages ? I see a comment ‘you can get that by implementing the same RNG as matlab/python in Julia in a package, but not in Base.’ and I am not sure how exactly to do that

Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 1:25pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/2 "2017-10-17T13:25:25Z")

</div>

You would either have to

1. implement the RNG of the other languages in Julia,
2. find a C/C++/Fortran library that does this, and write a thin wrapper in Julia.

Doing the first properly is not a trivial task, but it would teach you a lot about the internals of RNGs. Doing the second may be easier.

However, [Matlab documentation](https://www.mathworks.com/help/matlab/ref/rng.html) suggests that they also have a SIMD version, so you could use that and compare to Julia’s default RNG first.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 17, 2017, 1:33pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/3 "2017-10-17T13:33:49Z")

</div>

Why do you want to get the same random numbers in the two languages?

---

<div class="post-metadata">

**Author:** ![rohith14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohith14/32/2229_2.png) [@rohith14](https://discourse.julialang.org/u/rohith14)\
**Post date:** [October 17, 2017, 2:48pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/4 "2017-10-17T14:48:36Z")

</div>

For example, if i want to benchmark some sorting algorithms in different languages, i would start with same initial data-set. Other option is to use a fixed data-set (hard-coded).

---

<div class="post-metadata">

**Author:** ![rohith14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohith14/32/2229_2.png) [@rohith14](https://discourse.julialang.org/u/rohith14)\
**Post date:** [October 17, 2017, 2:49pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/5 "2017-10-17T14:49:27Z")

</div>

“However, Matlab documentation2 suggests that they also have a SIMD version, so you could use that and compare to Julia’s default RNG first”

I verified that and the numbers are different

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 17, 2017, 3:06pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/6 "2017-10-17T15:06:18Z")

</div>

I would suggest generating the test data in one language or the other and saving it as a .mat file (you can use MAT.jl to read mat files in Julia). That will give you reliably identical data which you can also save for posterity if you want.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 17, 2017, 3:10pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/7 "2017-10-17T15:10:51Z")

</div>

Alternatively, if it is feasible to test your sorting algorithm on thousands of randomly generated datasets, the average performance should be robust to the composition of your data.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 17, 2017, 8:33pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/8 "2017-10-17T20:33:15Z")

</div>

1. Find an independent random number generator written in C/C++, write a mex interface for it in Matlab and ccall it from Julia.

---

<div class="post-metadata">

**Author:** ![rohith14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohith14/32/2229_2.png) [@rohith14](https://discourse.julialang.org/u/rohith14)\
**Post date:** [October 18, 2017, 2:57pm UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/9 "2017-10-18T14:57:15Z")

</div>

Thank you all. I ended up using (rdeits suggestion)  
“I would suggest generating the test data in one language or the other and saving it as a .mat file (you can use MAT.jl to read mat files in Julia). That will give you reliably identical data which you can also save for posterity if you want.”

---

<div class="post-metadata">

**Author:** ![Gast](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gast/32/206425_2.png) [@Gast](https://discourse.julialang.org/u/Gast)\
**Post date:** [December 27, 2023, 12:54am UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/10 "2023-12-27T00:54:27Z")

</div>

I wonder if someone has made any progress on this. I have a similar issue to @rohith14 and found my way using @rdeits suggestion.

I am estimating a simple optimization algorithm using Julia’s `optimize(..., NelderMead())` from the package [‘Optim.jl’](https://julianlsolvers.github.io/Optim.jl/stable/) and MATLAB’s `fminsearch()`.

The estimation involves simulating random draws. The seed in Julia is through ` Random.seed!()` from [Random.jl](https://docs.julialang.org/en/v1/stdlib/Random/) whereas in MATLAB is set using `rand('state',...)`.

However, the only way to get results in Julia that are close to what I get in MATLAB is by importing the simulated data from MATLAB (through [MAT.jl](https://juliahub.com/ui/Packages/General/MAT/)).

Does anyone know if it is possible to get the same random numbers in both languages?

Thanks!

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 27, 2023, 3:13am UTC](https://discourse.julialang.org/t/rand-in-julia-vs-matlab/6505/11 "2023-12-27T03:13:33Z")

</div>

The Xoshiro-family pseudorandom generators are straightforward to implement, if you don’t need maximum performance. This is the reference implementation for xoshiro256++:

```c
uint64_t next(void) {
	const uint64_t result = rotl(s[0] + s[3], 23) + s[0];

	const uint64_t t = s[1] << 17;

	s[2] ^= s[0];
	s[3] ^= s[1];
	s[1] ^= s[2];
	s[0] ^= s[3];

	s[2] ^= t;

	s[3] = rotl(s[3], 45);

	return result;
}

```

So I guess you just need to implement it in Matlab, given that Julia comes with it in the `Random` stdlib. Start with the same seed in Julia and Matlab, of course.
