This can be done but may need some significant coding based upon experience of doing this sort of thing before (allocating people to training courses based upon their availability).

The basic idea is to work out the number of shifts that each person is available for. Sort these so that the least available person is first. Then allocate this person at random. Then take the next least available person and find a gap for them randomly selecting from the remaining places. Keep going until everyone is allocated or you reach a stalemate where there is no match. If the latter is the case, start the process again and keep going until you get a solution.