how to make randi and randint give out same data
47 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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]
0 个评论
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 Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!