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

Greg
Greg 2017년 1월 25일
Doesn't adding a constraint like "sum to less than 46" necessarily negate the randomness? I mean, going way beyond normal discussions of "pseudo-" random.
Further, if each element must be greater than 1, that inherently tells you the maximum array length is 45. (If you mean greater-than-or-equal-to, then it's 46).
Bill Symolon
Bill Symolon 2017년 1월 25일
It's the first part of a code for Bulgarian Solitaire; consisting of n cards, where n <= 45, divided into at least two piles. Once I get the initial array, I need to take one card from each pile and combine them into a new pile and repeat. For example: if n=15, the final result needs to be five piles consisting of 1, 2, 3, 4 and 5 cards, respectively. Just looking for some help on generating that initial array. Hope this helps clarify. Thanks.
Greg, yes, it IS possible to generate a set of numbers that are randomly distributed, subject to a constraint.
For example, consider the rows of the array:
A = rand(1e6,1);
A = [A;1-A];
The rows of A comprise a set that are uniformly distributed along the line segment that passes between the points {[0 1], [1,0]}. So, completely uniformly distributed (and random), yet constrained to lie along a line segment. You can also view it as random, yet subject to the constraint that the sum is 1.
But in order to generate a random sample, you need to understand/provide the distribution that will generate said sample. It is not enough to just say "random".
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일

0 개 추천

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일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2017년 1월 25일

답변:

2017년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by