Making random to work like randi

I have this code
idx = randi(N_clusters,N_clusters,1);
but I want to use the random function to achieve the same output. I do not get into why I want to use the random function but it is important for my work flow. So then I tried
pd = makedist('DiscreteUniform','Lower',1,'Upper',N_clusters);
idx = random(pd,N_clusters,1);
But MATLAB gives an error that "DiscreteUniform" is not recognized. What is my best alternative usinf the random function, if I stll can.

 Respuesta aceptada

Paul
Paul el 4 de Ag. de 2025
I wonder why makedist doesn't support Uniform Distribution (Discrete).
But random can be called with that distribution.
N_clusters = 10;
rng('default');
idx = randi(N_clusters,N_clusters,1)
idx = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rng('default');
random('Discrete Uniform',N_clusters,N_clusters,1)
ans = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

1 comentario

This
idx = random('Discrete Uniform',N_clusters,[N_clusters 1]);
seems to work. I wonder about the makedist too. This caused the whole confusion on my end.

Más respuestas (1)

Matt J
Matt J el 4 de Ag. de 2025
Editada: Matt J el 4 de Ag. de 2025
idx = floor( (N_clusters-1)*rand(N_clusters,1) )+1

11 comentarios

Snoopy
Snoopy el 4 de Ag. de 2025
Thanks but I need to use the random function not rand.
Matt J
Matt J el 4 de Ag. de 2025
Fine but random can imitate rand() using the 'Uniform' distribution selection (which does exist, unlike 'UniformDiscrete').
Snoopy
Snoopy el 4 de Ag. de 2025
Yes but I need to stick to the random function. I cannot use the rand function.
Matt J
Matt J el 4 de Ag. de 2025
Then replace the rand() function in my code with the random() function, invoked appropriately to create a uniform distribution.
Snoopy
Snoopy el 4 de Ag. de 2025
Simply replacing did not the trick if I am not overlooking.
Matt J
Matt J el 4 de Ag. de 2025
It should. We would have to see what you did.
This
idx = floor((N_clusters-1)*random(N_clusters,1))+1;
gives
Error using random (line 69)
Probability distribution name must be the name of a supported distribution.
AI suggested
pd = makedist('DiscreteUniform','Lower',1,'Upper',N_clusters);
idx = random(pd,N_clusters,1);
but that also does not work.
Torsten
Torsten el 4 de Ag. de 2025
Editada: Torsten el 4 de Ag. de 2025
These are the distributions that are available for the "makedist" command:
list = makedist
list = 29×1 cell array
{'beta' } {'binomial' } {'birnbaumsaunders' } {'burr' } {'exponential' } {'extremevalue' } {'gamma' } {'generalizedextremevalue'} {'generalizedpareto' } {'halfnormal' } {'inversegaussian' } {'logistic' } {'loglogistic' } {'lognormal' } {'loguniform' } {'multinomial' } {'nakagami' } {'negativebinomial' } {'normal' } {'pearson' } {'piecewiselinear' } {'poisson' } {'rayleigh' } {'rician' } {'stable' } {'tlocationscale' } {'triangular' } {'uniform' } {'weibull' }
Snoopy
Snoopy el 4 de Ag. de 2025
Yes, that is the problem and hence I am seeking a solution to that, using still the random function.
Matt J
Matt J el 4 de Ag. de 2025
Editada: Matt J el 4 de Ag. de 2025
AI suggested
But that's not what I suggested to you earlier. I suggested 'Uniform'
N_clusters = 10;
rng('default');
pd = makedist('Uniform','Lower',1,'Upper',N_clusters+1);
idx = floor(random(pd,N_clusters,1))
idx = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

This question is locked.

Categorías

Más información sobre Random Number Generation en Centro de ayuda y File Exchange.

Productos

Versión

R2025a

Preguntada:

el 4 de Ag. de 2025

Locked:

el 4 de Ag. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by