# Creating a Random list with constraints

I need something similar to the List below, but not totally random and values within each individual column and row remaining the same:

xyCoord = RandomReal[{-2, 25}, {270, 219, 2}];


The above is not what is required. I need

{{x1, y1}, {x1, y2}.........{x1,y219}}, {{x2, y1,}, {x2, y2}.........., {x2, y219}},..............,{{x270, y1},.........................,{x270, y219}}

I need x1 > x2> x3...... > x270. Same for y need y1 > y2 > y219.

Each xi and yi (i being index number) is selected randomly within these constraints.

The final list should have the following dimensions.....

Dimensions[xyCoord] = {270, 219, 2}

• Do you want a sorted list as already provided, or do you want the random number generated at each step to be generated in the range {lastX, 25}? i.e. do you want xcoord =Flatten@NestList[ RandomReal[{Last@#, 25}, 1] &, {-2}, 270] similarly for y, then proceed as JasonB suggested. Commented May 13, 2016 at 16:24

Your command will generate 270*219*2 = 118260 random numbers, but from your description, you only want 270+219 random numbers. This should work,

xcoord = RandomReal[{-2, 25}, 270];
ycoord = RandomReal[{-2, 25}, 219];
xycoord = Outer[List, Reverse@Sort@xcoord, Reverse@Sort@ycoord];


Here is an example output if I change the 270 and 219 to 10 and 10

• Tuples[] seems like a better choice here. Commented May 13, 2016 at 14:03
• You mean like Tuples[{Sort@xcoord, Sort@ycoord}]? But then you would need to use Partition to shape it into a three-dimensional array right? Commented May 13, 2016 at 14:08
• Huh, I didn't notice that the OP wanted a nested list. Sorry 'bout that... Commented May 13, 2016 at 14:09
xcoord = Reverse@Sort@RandomReal[{-2, 25}, 270];
ycoord = Reverse@Sort@RandomReal[{-2, 25}, 219];
Table[{x, y} , {x, xcoord}, {y, ycoord}]


Another way is this (as mentioned in comments):

Partition[ Tuples[{xcoord, ycoord}] , Length@ycoord]


which is useful to know especially if you want to flatten the list anyway you can just use Tuples[{xcoord, ycoord}]

• Well done. Good Answer. Although, you forgot the sort@ on the ycoord.
– SPIL
Commented May 13, 2016 at 15:06
• oops, fixed.... Commented May 13, 2016 at 15:59