how can i build a function that can generate 5 random numbers
Show older comments
is there a way to build a function() that once called, it will give in response 5 random numbers between 1 and 40, even if its a matrix?
3 Comments
Paolo
on 29 May 2018
mihaita98
on 29 May 2018
Walter Roberson
on 29 May 2018
Are you wanting to iterate through all of the combinations of 40 items taken 5 at a time? There are 658008 of those. Or do you want all of the permutations of 40 items taken 5 at a time? There are 6799294027065814452880093913300965785600000000 of those.
When you say you want different numbers between consecutive calls, then do all of the numbers have to be different, or do you just not want the same list of numbers twice in a row? For example would [3 9 10 27 31] followed by [1 9 27 30 38] be acceptable ?
Accepted Answer
More Answers (1)
Walter Roberson
on 29 May 2018
If you just need to do this once then
randperm(40, 5)
If you need to create a number of these then there is a way using sort() but no direct call for it.
1 Comment
Image Analyst
on 29 May 2018
Also to expand on that, randperm() gives random integer numbers without repeated numbers being possible. randi() will give random integers but repeats are possible.
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!