Effacer les filtres
Effacer les filtres

Loop / Repeat if random number = 0

2 vues (au cours des 30 derniers jours)
Mark
Mark le 12 Juin 2012
I have good and fun code and it is working very well.
However, I am getting an error when I get zero (0) from my "random integer generator":
n=round(200.*rand)
Is there any way I can tell the script to rerun the calculation for n, if n=zero? I am unable to change the variable n, and despite the low chance of getting two n=0 I want to "reroll" until n is definitely not equal to zero. Any idea how to do this?

Réponse acceptée

Kye Taylor
Kye Taylor le 12 Juin 2012
Try
n = round(200*rand);
while (n == 0)
n = round(200*rand);
end
Or, even better,
n = randi(200); % generates random integer between 1 and 200
  1 commentaire
Greg Heath
Greg Heath le 12 Juin 2012
n = 1+round(199*rand)
Greg

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by