how to make randi and randint give out same data
49 ビュー (過去 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!