how to make randi and randint give out same data

34 views (last 30 days)
As new matlab version does not support randint, I need to use randi. But I want the data generated by them to be same.
Old matlab:
randint(1,10,10,0)
output = [9 2 6 4 8 7 4 0 8 4]
New matlab:
rng(0); randi([0 9],1,10)
ouput = [8 9 1 9 6 0 2 5 9 9].
How one has to use randi function to generate same data as randint. (ofcourse by using same seed/state).
Thanks in advance.

Answers (2)

Adam Danz
Adam Danz on 5 May 2020
Edited: Adam Danz on 6 May 2020
Note the difference in syntax between the new and old random integer functions
Examples
I don't know how the "output" vector was produced in your question but the modern methods of produced seeded random integers do not reproduce those values.
% randint:
% inputs 1 & 2 define size, input 3 is 1+max value,
% input 4 is the random num gen seed using Mersenne Twister algorithm
x1 = randint(1,10,10,0);
x1 = [9 2 6 4 8 7 4 0 8 4] % Based on content from within the question, **not tested**.
% randi:
% set random num gen seed and generator
rng(0, 'twister')
% Input 1 is max value, input 2 defines size
x2 = randi(9,[1,10]);
x2 = [8 9 2 9 6 1 3 5 9 9];
randint set the random number generator seed using the outdated 'seed' feature of the rand function. It still works today but is not recommended. It also doesn't reproduce the results you shared in the question.
rand('twister',0) % not recommended
x2 = randi(9,[1,10]);
x2 = [5 7 6 5 4 6 4 9 9 4]

HONG CHENG
HONG CHENG on 28 Apr 2022
You just need to change the order of paramters
a=randint(3,4,[1,4]);
a=randi([1,4],3,4);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!