Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:52:46+00:00 2026-06-04T20:52:46+00:00

I am generating data in R and Matlab for 2 separate analyses and I

  • 0

I am generating data in R and Matlab for 2 separate analyses and I want to determine if the results in the two systems are equivalent. Between the 2 sets of code there is inherent variability due to the random number generator. If possible, I would like to remove this source of variability. Does anyone know of a way to set the same starting seed in both Matlab and R? I provide some demo code below.

%Matlab code
seed=rng %save seed
matlabtime1=randn(1,5) %generate 5 random numbers from standard normal 
rng(seed) %get saved seed
matlabtime2=randn(1,5) %generates same output as matlabtime1

#R code
set.seed(3) #save seed
r.time1=rnorm(5) #generate 5 random numbers from standard normal 
set.seed(3) #get saved seed
r.time2=rnorm(5) #generates same output as r.time1

Essentially, I want the results from matlabtime2 and r.time2 to match exactly. (The code I am using is more complex than this illustrative demo so rewriting in one language only is not really a feasible option.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:52:47+00:00Added an answer on June 4, 2026 at 8:52 pm

    I’m finding it difficult to get the same random numbers in R and
    MATLAB – even using the same seed for the same algorithm (Mersenne
    Twister).

    I guess it’s about how they are implemented – even with the same seed, they have different initial states (you can print and inspect the states both in R and MATLAB).

    In the past when I’ve needed this, I generated random input, saved it as a file on disk, and fed it to both MATLAB and R.

    Another option is to write C wrappers for a random number generator (there are many of these in C/C++) both for R and MATLAB and invoke those instead of the built-in ones.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two programs - one of them is generating some data and put
i am generating *.reg file using code which will have some important data. and
I'm using the PRTools MATLAB library to train some classifiers, generating test data and
I'm generating a data feed on the local machine, that I want to pipe
I'm looking for the best approach for generating data transfer objects from business objects
I have a loop which is generating some data and in certain cases I
Generating normal columnar data in excel file is quite easy but does any one
I'm looking at MeVisLab's interface for generating views of data and I find that
I am working on a Hadoop project and generating lots of data in my
I am generating a map from meta data from a series of posts in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.