how to make randi and randint give out same data

조회 수: 87 (최근 30일)
manoj kumar medam
manoj kumar medam 2020년 5월 5일
댓글: Puput Dani 2023년 1월 28일
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.

답변 (2개)

Adam Danz
Adam Danz 2020년 5월 5일
편집: Adam Danz 2020년 5월 6일
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 2022년 4월 28일
You just need to change the order of paramters
a=randint(3,4,[1,4]);
a=randi([1,4],3,4);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by