273 views (last 30 days)
Show older comments
Tina Huynh on 6 Mar 2017
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand
Commented: Walter Roberson on 31 May 2024 at 18:14
Accepted Answer: Walter Roberson
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 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Stephen23 on 6 Mar 2017
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand#comment_434629
Edited: Stephen23 on 7 Mar 2017
"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 on 10 Nov 2021
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand#comment_1825719
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 on 10 Nov 2021
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand#comment_1826594
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.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Walter Roberson on 12 Oct 2019
Edited: Walter Roberson on 31 May 2024 at 18:13
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 Comments Show NoneHide None
Show NoneHide None
Alessandro on 31 May 2024 at 9:28
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand#comment_3176766
@Walter Roberson you can't have standard deviation 0. Maybe you meant 1
Walter Roberson on 31 May 2024 at 18:14
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/328427-how-do-i-use-randn-vs-randi-vs-rand#comment_3177071
@Alessandro
Corrected, thanks!
Sign in to comment.
More Answers (2)
Jan on 7 Mar 2017
Edited: Jan on 7 Mar 2017
Open in MATLAB Online
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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
MANISH BANSAL on 27 Aug 2020
Use the documentation for undestanding the Functions of the MATLAB by typing doc rand or doc randi
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABMathematicsRandom Number Generation
Find more on Random Number Generation in Help Center and File Exchange
Tags
- random number generator
- random
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office