Can the random function accommodate the randi function?

38 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord ungefär 19 timmar ago
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 Comments
Steven Lord
Steven Lord ungefär 19 timmar ago
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.
Snoopy
Snoopy ungefär 18 timmar ago
Edited: Snoopy ungefär 18 timmar ago
What an elaborate and excellent answer. Thank you. I can imagine the question and the answer will benefit the community. Thanks also for the answer above.

Sign in to comment.

More Answers (1)

dpb
dpb ungefär 19 timmar ago
random('unid',2,[1,10])-1
ans = 1×10
1 0 0 1 0 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
nnz(ans)
ans = 7
which -all random
/MATLAB/toolbox/stats/stats/random.m /MATLAB/toolbox/stats/stats/@gmdistribution/random.m % gmdistribution method /MATLAB/toolbox/stats/stats/@piecewisedistribution/random.m % piecewisedistribution method
which -all rand
built-in (/MATLAB/toolbox/matlab/randfun/rand) /MATLAB/toolbox/matlab/randfun/@RandStream/rand.m % RandStream method /MATLAB/toolbox/parallel/array/distributed/@codistributed/rand.m % codistributed method /MATLAB/toolbox/parallel/array/distributed/@distributed/rand.m % distributed method /MATLAB/toolbox/parallel/array/distributed/@codistributor2dbc/rand.m % codistributor2dbc method /MATLAB/toolbox/parallel/array/distributed/@codistributor1d/rand.m % codistributor1d method /MATLAB/toolbox/parallel/gpu/gpu/@gpuArray/rand.m % gpuArray method
which -all randi
built-in (/MATLAB/toolbox/matlab/randfun/randi) /MATLAB/toolbox/matlab/randfun/@RandStream/randi.m % RandStream method /MATLAB/toolbox/parallel/array/distributed/@codistributed/randi.m % codistributed method /MATLAB/toolbox/parallel/array/distributed/@distributed/randi.m % distributed method /MATLAB/toolbox/parallel/array/distributed/@codistributor2dbc/randi.m % codistributor2dbc method /MATLAB/toolbox/parallel/array/distributed/@codistributor1d/randi.m % codistributor1d method /MATLAB/toolbox/parallel/gpu/gpu/@gpuArray/randi.m % gpuArray method
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.
OTOH, rand and randi are builtins in the base MATLAB product.

Community Treasure Hunt

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

Start Hunting!