# how to make randi and randint give out same data

65 Ansichten (letzte 30 Tage)
manoj kumar medam am 5 Mai 2020
Kommentiert: Puput Dani am 28 Jan. 2023
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).
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (2)

Adam Danz am 5 Mai 2020
Bearbeitet: Adam Danz am 6 Mai 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]
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

HONG CHENG am 28 Apr. 2022
You just need to change the order of paramters
a=randint(3,4,[1,4]);
a=randi([1,4],3,4);
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Puput Dani am 28 Jan. 2023
XIE XIE

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by