C2 and copy down/right:
Formula:
=RANDBETWEEN(MAX(0,2*$B3-SUM($B3:B3)-SUM(D$2:$L$2)),MIN(C$2,2*$B3-SUM($B3:B3)))
Note that for students: 8, 17, 18 you will get errors, because they collected 60 points, while max available total was 56 - please next time prepare REPRESENTATIVE sample data.
Also note that this formula refers one column right from your table, (column L), so for second sheet it shall be:
Formula:
=RANDBETWEEN(MAX(0,2*$B3-SUM($B3:B3)-SUM(D$2:$H$2)),MIN(C$2,2*$B3-SUM($B3:B3)))
(up to column H)
Bookmarks