Selecting random cells based on criteria

Hi I have a sheet that contains a few thousand entries.

In column "D" I have a value assigned to each entry (A, - W)

What I want to do is select a random set of entries for each of the criteria in column "D" and return a list of cell values that are in column "A"

However the number of cells I want to return changes for each criteria

So for example If cell in column "D" contains an "A" I want to return the value of the corresponding cell in Column "A".

So far so good?

The next twist is that there is a variable number of entries for each criteria (A, B, C, etc... ,W)

So if there are a total of 56 entries with an "A" in column "D" I want to specify the number of random entries I want returned for example I only want 9 of the 56.

If that hasn't totally confused everyone, any suggestions on how to do this would be welcome.