how to make an array which gives random value all the time u print the array

26 ビュー (過去 30 日間)
Krishna
Krishna 2025 年 10 月 7 日 6:59
回答済み: dpb 2025 年 10 月 7 日 19:32
basically i need a code which gives me random values in an array all the time
for eg;
a=[1 4 63 75]
a
a=[3 32 54 23]
  1 件のコメント
Stephen23
Stephen23 2025 年 10 月 7 日 7:02
編集済み: Stephen23 2025 年 10 月 7 日 9:48
What distribution? What range?
Do you want to generate new values only when DISPLAY is called (as you wrote), or any time that the array is accessed: creation, indexing, displaying, conversion to other types, etc.?
Is there a particular reason why you cannot simply call RANDI directly ?

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2025 年 10 月 7 日 19:32
Without all the details of just what, precisely, is wanted, the basic idea would be to wrap the RNG call into a function so you don't have to call the RNG every time directly...
function res=getnewarray()
% return set of four (4) random integers on range 1-100 as row vector
res=randperm(100,4); % without repeitions comment out if rather have
%res=randi(100,1,4); % repitions possible (not likely, but allowed)
end
a=getnewarray
a = 1×4
46 18 15 73
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a=getnewarray
a = 1×4
35 17 43 33
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a=getnewarray
a = 1×4
98 77 37 46
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Modify to suit -- any distribution can be set or can add arguments to allow changing number or parameters.
If it is more complicated, it might be desirable to build a class with methods defined for needed operations.

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by