![]() ![]() See that these formulas recover p and q from the unrolled elements in k. We can think of them as the elements in a strictly upper triangular array of size m by m, thus those elements above the main diagonal. ![]() Thus the first few pairs in that list are: Now, if we associate each possible pair with one of the integers in k, we can work backwards, from the integers generated in k, to a pair. This avoids precision problems for some values of m near the upper limits.) (Note that I've written this expression with m in a funny way, dividing by 2 in perhaps a strange place. (Older matlab releases do not allow the second argument to randperm.) k = randperm(m/2*(m-1),n) So we could generate n random integers in the range. (I.e., the sum of the numbers from 1 to m-1.) How many possible pairs are there? The total number of pairs is just m*(m-1)/2. You wish to generate n pairs of integers,, such that p and q lie in the interval, and p Is there an easier, cleaner way of generating unique pairs of distinct integers?Įasy, peasy, when viewed in the proper way. I considered generating a load of random numbers randi(m, ), and repeatedly rejecting the rows which repeated, but I was concerned about getting stuck in a loop when n was close to m(m-1)/2. upwards of 10,000), MATLAB runs out of memory. However, the matrix A is of size m x m, meaning when m becomes large (e.g. Idx_to_use = randperm( size(all_pairs, 1), n ) % choosing random n pairs (Obviously a constraint on the parameters is that n mĪll_pairs = % this contains all possible pairs is ok but would be rejected.ĮDIT: Each possible pair should be chosen with equal likelihood. Īlso, each pair should consist of two distinct integers i.e. For uniqueness, I consider the order of the numbers in the pair to be irrelevant such that is equal to. In MATLAB, I would like to generate n pairs of random integers in the range, where each pair is unique. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |