Generate random integer array
Mostrar comentarios más antiguos
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 comentarios
Greg
el 25 de En. de 2017
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
el 25 de En. de 2017
John D'Errico
el 25 de En. de 2017
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
el 25 de En. de 2017
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
el 25 de En. de 2017
Editada: Bill Symolon
el 25 de En. de 2017
Respuesta aceptada
Más respuestas (1)
Steven Lord
el 25 de En. de 2017
0 votos
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.
Categorías
Más información sobre Random Number Generation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!