Generate random integer array

1 次查看(过去 30 天)
I need to create an array of random integers. The array must consist of at least two numbers greater than one but there is no maximum array length. The elements of the array need to sum to less than 46. Thanks for your help.
  5 个评论
John D'Errico
John D'Errico 2017-1-25
Bill -
If you know n in advance, then this is pertinent information, although still insufficient. n is not simply random. It is arbitrary, but known.
What do you do when n is 16? You need to explain these things, otherwise, one is forced to guess what you want to do.
So can we assume that n is given by the user. Then you want to come up with the number of cards in each pile, that then sums to n? Must each number be greater than 2? How large can any single pile of cards then be?
Bill Symolon
Bill Symolon 2017-1-25
编辑:Bill Symolon 2017-1-25
John,
I see what your asking and I should have been more clear. Sorry, I'm still new at this.
Let's assume that the user inputs n which is an arbitrary integer between 1 and 45, inclusive. The code should then "randomly" parse the input into an array of smaller integers. There are no restrictions on the length of the array or the size of each element, except that the elements need to be integers and need to sum to n.
Hopefully that's a bit more clear. Thanks for your help.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2017-1-25
I was about the say the same thing as Steven. Because it's card dealing, you need to use randperm(), not randi(). See my two attached car dealing demos.

更多回答(1 个)

Steven Lord
Steven Lord 2017-1-25
If you're looking to shuffle a deck of cards, use randperm. Once you have a vector representing the shuffled deck of cards, use indexing to extract subsets of the vector to represent each of your piles.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by