MATLAB Answers

How Matlab calculate randsample using randsample function

24 views (last 30 days)
riad didou
riad didou on 14 Jul 2020 at 20:20
Commented: Adam Danz on 14 Jul 2020 at 21:41
I need to understand the method of random selection (without replacement) (MATLAB 2019b); i mean behind this function how the algorithm works?
Knowing that i have used this:


Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 14 Jul 2020 at 20:38
Edited: Adam Danz on 14 Jul 2020 at 21:24
That syntax merely takes a random permutation of your data.
So the question becomes, how does the randperm function work?
You're in luck because it's explaind in the randperm documentation page. See the "Tips" section.
You'll see that randperm uses a uniform pseudorandom number generator that can have different internal settings and states. There are several types of random number generators. See the table:
To determine which one you're using, run
Example output
>> rng
ans =
struct with fields:
Type: 'twister' % <------
Seed: 9999
State: [625×1 uint32]
For example, "twister" is defined by the mt19937ar generator which is fully described in the link above and contains citations to primary literature, depending on how far down the rabbit hole you want to go.
To summarize, the randsample(n,k) syntax merely subsamples your data using randperm without replacement. The random selection within randperm is controlled by the random number generator you're using which can be determined by running rng().
Also see this general list of topics regarding Matlab random number generation.


Show 1 older comment
Adam Danz
Adam Danz on 14 Jul 2020 at 21:21
I'm not sure what I could add without repeating myself.
The 2nd link covers the different internal settings and states.
The rng function tells you what settings you're using.
Then you can go back to the 2nd link and read some more about the internal states you're using. There are footnotes in that link pointing to the primary literature behind each method.
That's the path you need to go down to understand it deeper. I'd be glad to discuss more about something specific.
I'll add this link to my answer as well; it's a general list of topics on Matlab rng.
Adam Danz
Adam Danz on 14 Jul 2020 at 21:41
No problem. I'm not sure what you're expecting to find.
If you're trying to reproduce randomized results, you just need to set the rng seed at the beginning of your function / script.

Sign in to comment.

More Answers (0)