Effacer les filtres
Effacer les filtres

generate random numbers in range from (0.8 to 4)

397 vues (au cours des 30 derniers jours)
Jamal Ahmad
Jamal Ahmad le 11 Mar 2013
Commenté : Image Analyst le 20 Jan 2021
Hi How to generate 20 random numbers in range from (0.8 to 4) Thanks
  1 commentaire
Jan
Jan le 6 Nov 2017
Modifié(e) : Jan le 6 Nov 2017
@Arnab: See Azzi's and Image Analyst's answers, which contains exactly the same.

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 11 Mar 2013
Modifié(e) : Azzi Abdelmalek le 11 Mar 2013
xmin=0.8
xmax=4
n=20
x=xmin+rand(1,n)*(xmax-xmin)
  3 commentaires
Steven Lord
Steven Lord le 19 Jan 2021
xmin=0.8;
xmax=4;
n = 1e6; % Generating a few more numbers than the original n=20;
x=xmin+rand(1,n)*(xmax-xmin);
histogram(x, 0.8:0.1:4.1)
Looks reasonably uniform to me.
Image Analyst
Image Analyst le 20 Jan 2021
If you want it EXACTLY perfectly uniform (which is doubtful), then you should use linspace() instead of rand().

Connectez-vous pour commenter.

Plus de réponses (3)

Image Analyst
Image Analyst le 11 Mar 2013
Did you look in the help? You will see the first example does what you want:
Example 1
Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);
  1 commentaire
Jan
Jan le 11 Mar 2013
Modifié(e) : Jan le 6 Nov 2017
Suggesting to read the help text is a very strong idea, because it helps in nearly all future problems also.

Connectez-vous pour commenter.


ChristianW
ChristianW le 11 Mar 2013
n = 20;
R = [0.8 4];
z = rand(n,1)*range(R)+min(R)

Ka Mirul
Ka Mirul le 17 Oct 2017

Hallo, I have seen a tutorial about "Generating Random Number in Specific Range" at https://www.youtube.com/watch?v=MyH3-ONYL_k and it works. Hope it will help you.

Catégories

En savoir plus sur Random Number Generation 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