How do I use randn vs randi vs rand?
189 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tina Huynh
el 6 de Mzo. de 2017
Comentada: Walter Roberson
el 31 de Mayo de 2024
I want a 3x5 matrix of random integers between 5 and 10. So I typed the following:
randi ([5,10], 3, 5) and this worked perfectly fine.
When I wanted a 3x5 matrix of random real numbers between 5 and 10, I assumed I would use randn and type:
randn ([5,10], 3,5) but it kept coming up as an error.
Can someone explain to me what I'm doing wrong? I'm just learning how to use MatLab.
3 comentarios
CL
el 10 de Nov. de 2021
I only understand the difference between rand(), randi(), randn after reading this post and specifically Walter's answers. Only then does the documentation and help partially make sense. People already familiar with the material may read the docs with ease but for students it often quite challenging to dymistify any documentation. As simple as randi(), the doc somehow starts with this paragraph:
randi Pseudorandom integers from a uniform discrete distribution. R = randi(IMAX,N) returns an N-by-N matrix containing pseudorandom integer values drawn from the discrete uniform distribution on 1:IMAX.
What is Pseudorandom? what is uniform? What is "drawn from 1:MAX"?
Almost every docs are mean to be written correctly rather than actually helping students to learn. Telling people to read the docs it's like telling a kid to lookup a dictionary and stop asking what a word means
Walter Roberson
el 10 de Nov. de 2021
What is Pseudorandom
MATLAB does not have any true random number generators. It turns out to be fairly difficult for a computer to internally generate true random numbers that have good statistical properties. So what nearly everyone uses instead is algorithms that mimic true random numbers, but in a way that is repeatable. Because the results appear to be random but are not really, they are called "pseudo-random". "The prefix pseudo- (from Greek ψευδής, pseudes, "lying, false") is used to mark something that superficially appears to be (or behaves like) one thing, but is something else."
what is uniform
This describes a fundamental property associated with any random number generator dealing with the random probabilities. It is so fundamental, that it is nearly to the point where if you do not know what uniform is for randomness, you probably should not be using a random number generator.
Respuesta aceptada
Walter Roberson
el 12 de Oct. de 2019
Editada: Walter Roberson
el 31 de Mayo de 2024
rand() : creates uniform random numbers ("with replacement") in the range (0,1) exclusive. To create uniform random numbers in the range (a,b) exclusive, use rand()*(b-a)+a . The only arguments for rand() are the sizes of the resulting array.
randn(): creates random number on the normal distribution ("with replacement") with mean 0 and standard deviation 1. To create normally distributed random numbers with mean a and standard deviation b, use randn()*b + a . The only arguments for randn() are the sizes of the resulting array.
randi(): creates uniform distributed random integers ("with replacement") in a range. If the first argument is a scalar, the range is 1 to that scalar. If the first argument is a vector of length 2, then the range is from the first integer to the second integer. The arguments after the first one are the sizes of the resulting array.
If you need uniform random integers without replacement on the range [a b] then use randperm(b-a+1)+a-1
2 comentarios
Alessandro
el 31 de Mayo de 2024
@Walter Roberson you can't have standard deviation 0. Maybe you meant 1
Más respuestas (2)
Jan
el 7 de Mzo. de 2017
Editada: Jan
el 7 de Mzo. de 2017
Reading the documentation should be the first step:
doc rand
doc randn
You find a solution for your problem in the first one: https://www.mathworks.com/help/matlab/math/floating-point-numbers-within-specific-range.html
Whenever you get an error and post a corresponding question in the forum, insert a copy of the complete error message.
0 comentarios
MANISH BANSAL
el 27 de Ag. de 2020
Use the documentation for undestanding the Functions of the MATLAB by typing doc rand or doc randi
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!