Minecraft wiki entry for Command_blocks_and_functions, associated text: /tag @r add cookie
/give @a[tag=cookie] minecraft:cookie 5
/say @a[tag=cookie] has been given 5 cookies!
/tag @a[tag=cookie] remove cookie