How do I use randn vs randi vs rand?

357 ビュー (過去 30 日間)
Tina Huynh
Tina Huynh 2017 年 3 月 6 日
コメント済み: Walter Roberson 2021 年 11 月 10 日
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 件のコメント
CL
CL 2021 年 11 月 10 日
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
Walter Roberson 2021 年 11 月 10 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 12 日
編集済み: Walter Roberson 2020 年 8 月 27 日
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 0. 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 件)

Jan
Jan 2017 年 3 月 7 日
編集済み: Jan 2017 年 3 月 7 日
Reading the documentation should be the first step:
doc rand
doc randn
Whenever you get an error and post a corresponding question in the forum, insert a copy of the complete error message.

MANISH BANSAL
MANISH BANSAL 2020 年 8 月 27 日
Use the documentation for undestanding the Functions of the MATLAB by typing doc rand or doc randi

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by