How do I use randn vs randi vs rand?
Ältere Kommentare anzeigen
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 Kommentare
"Can someone explain to me what I'm doing wrong?"
The best place to learn how to use MATLAB is by reading the documentation. And when you read the documentation then you will learn that each of those functions has different syntaxes. For example, the randn documentation clearly states that it generates numbers distributed with the standard normal distribution. It does not mention anywhere that limits for those numbers can be specified (and this would would contradict the definition of the standard normal distribution anyway).
Summary: different functions have different help pages showing different syntaxes using different inputs to give different outputs. Always read the documentation.
CL
am 10 Nov. 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
am 10 Nov. 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.
Akzeptierte Antwort
Weitere Antworten (2)
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.
MANISH BANSAL
am 27 Aug. 2020
0 Stimmen
Use the documentation for undestanding the Functions of the MATLAB by typing doc rand or doc randi
Kategorien
Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!