Can the random function accommodate the randi function?
43 ビュー (過去 30 日間)
古いコメントを表示
I am using
randi([0,1],[N,1]);
to randomly generate 0s and 1s. If I am not mistaken, the
random
function is replacing the older, distribution specific, functions such as the
normrnd
function. I welcome this consolidation of random number generators under the
random
function. My question is whether I can use the
random
function in some way to replace the
randi
function that I state at the top of this message.
0 件のコメント
採用された回答
Steven Lord
2024 年 11 月 22 日 19:09
I would not position random as replacing the more specific pseudorandom number generation functions. See the Alternative Functionality section on that documentation page. The random function allows you to switch from which distribution you're drawing at run-time by passing a different input argument into the function rather than modifying your code. If you know which distribution you want to draw from at write-time you could call the specific <distribution>rnd function or (for certain distributions) a function in MATLAB as opposed to Statistics and Machine Learning Toolbox (betarnd for the Beta distribution, unifrnd from Statistics and Machine Learning or rand from MATLAB for continuous uniform distribution, etc.)
You can't exactly specify the discrete uniform distribution on [0, 1] with just a call to random, but you can specify the discrete uniform distribution on [1, N] by specifying 'unid' as the distribution and N as the parameter.
x = random('unid', 2, [1 1e5]); % 1e5 uniform numbers that are either 1 or 2
figure
histogram(x)
title('Discrete uniform numbers either 1 or 2')
What happens if you subtract 1 from the output of that random call? That will give you discrete uniformly distributed numbers from the set [0, N-1].
figure
histogram(x-1)
title('Discrete uniform numbers either 0 or 1')
Compare with randi:
y = randi([0 1], 1, 1e5); % different set of random numbers either 0 or 1
figure
histogram(y)
title("Uniform random numbers either 0 or 1")
Those look pretty uniformly distributed to me. Note that the second and third histograms are not necessarily identical. If you flip a coin twice then flip it twice more, the results of the latter two flips may not be identical to the results of the first two flips.
3 件のコメント
Steven Lord
2024 年 11 月 22 日 19:55
Does this mean that distribution specific random generators are not outdated so that my impression of the random function replacing them is wrong?
Correct. In fact, the random function calls the distribution-specific generator functions.
If yes, why did MATLAB create the random function then?
Keep in mind that the random function (introduced prior to release R2006a) may predate some of those distribution-specific generator functions. So perhaps your question should be "Why did we add those distribution-specific generator functions when random already existed?"
One of those reasons, as I mentioned, is to allow users to specify the generator at run-time, by changing the input argument to random rather than modifying the code to change which function gets called.
It's similar to how something like ode45 works. Imagine how annoying it would be if when you wanted to change the system of ODEs you're solving you had to edit the ode45.m function included in MATLAB and change the definition of the ODE function? Instead, all you have to do is change the input argument with which you call ode45 and it evaluates that function.
I thought it was a consolidation, a tidying up of all distribution specific random number generators.
No. It's more of a switchyard, a way of providing a common interface to the various distribution-specific generators. As another analogy, the distribution-specific generator functions are like a butcher's shop, a bakery, and a vegetable stand. If you know you want a cake or a loaf of bread, you can go directly to the bakery without having to bother with the butcher's or the vegetable stand.
random is more like the supermarket. You can get meat, bread, cake, or veggies at the supermarket depending on which department you visit. And if you change your mind (you decide to get a loaf of bread for a peanut butter and jelly sandwich instead of vegetables for a stir fry for lunch or dinner) you don't have to go to a completely different store.
And finally, why is the random function supposed to run slower?
With apologies to Uncle Ben and Stan Lee, with great(er) flexibility can come greater complexity. If you were to call say betarnd, MATLAB knows that's the function that needs to be called. It can almost immediately start generating the random numbers. If you say random('beta', ...) it needs to check that the input corresponds to a distribution it knows about, figure out that 'beta' means call betarnd, then call betarnd. That overhead of checking is not that expensive in the grand scheme of things, but it's not free.
In the analogy above, as soon as I step into the vegetable stand (if I want to buy veggies) I can start shopping. In the supermarket, I may need to walk a little further through or around different sections to reach the produce section. I'll get veggies both ways, but perhaps with a little more walking in the supermarket.
その他の回答 (1 件)
dpb
2024 年 11 月 22 日 19:05
random('unid',2,[1,10])-1
nnz(ans)
which -all random
which -all rand
which -all randi
The deal is that random is only available if one has the Statistics TB; and the "Alternative Functionality" section of the doc states that "random is a generic function that accepts either a distribution by its name name or a probability distribution object pd. It is faster to use a distribution-specific function, ...". So, while it is possible to use the one generic name, unless you're writing code that dynamically changes a distribution family, it would seem better to forego its use for the specific distribution desired.
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!