How to give random dyes in minecraft [duplicate]

I want to give the player a random dye when a player steps on a command block. For eg

/give @p dye

gives the player only the Ink Sac dye but I want to give a random dye to the player.

Optional: Better if it requires only one command block. Also helps if I can extend it to heads(/give @p skull).


You can select a random entity and base the /give command off of the result.

Initialization: /summon armor_stand ~ ~ ~ {CustomName:random0,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random1,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random2,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random3,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random4,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random5,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random6,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random7,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random8,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random9,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random10,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random11,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random12,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random13,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random14,Tags:[random]} /summon armor_stand ~ ~ ~ {CustomName:random15,Tags:[random]}

Commands when on pressure plate: /scoreboard players tag @r[type=armor_stand,tag=random] add selected /execute @e[type=armor_stand,tag=selected,name=random1] ~ ~ ~ give @p dye 1 0 /execute @e[type=armor_stand,tag=selected,name=random1] ~ ~ ~ give @p dye 1 1 /execute @e[type=armor_stand,tag=selected,name=random2] ~ ~ ~ give @p dye 1 2 /execute @e[type=armor_stand,tag=selected,name=random3] ~ ~ ~ give @p dye 1 3 /execute @e[type=armor_stand,tag=selected,name=random4] ~ ~ ~ give @p dye 1 4 /execute @e[type=armor_stand,tag=selected,name=random5] ~ ~ ~ give @p dye 1 5 /execute @e[type=armor_stand,tag=selected,name=random6] ~ ~ ~ give @p dye 1 6 /execute @e[type=armor_stand,tag=selected,name=random7] ~ ~ ~ give @p dye 1 7 /execute @e[type=armor_stand,tag=selected,name=random8] ~ ~ ~ give @p dye 1 8 /execute @e[type=armor_stand,tag=selected,name=random9] ~ ~ ~ give @p dye 1 9 /execute @e[type=armor_stand,tag=selected,name=random10] ~ ~ ~ give @p dye 1 10 /execute @e[type=armor_stand,tag=selected,name=random11] ~ ~ ~ give @p dye 1 11 /execute @e[type=armor_stand,tag=selected,name=random12] ~ ~ ~ give @p dye 1 12 /execute @e[type=armor_stand,tag=selected,name=random13] ~ ~ ~ give @p dye 1 13 /execute @e[type=armor_stand,tag=selected,name=random14] ~ ~ ~ give @p dye 1 14 /execute @e[type=armor_stand,tag=selected,name=random15] ~ ~ ~ give @p dye 1 15

You have to replace @p with a selector that targets the player that you want to give the item to, of course.


First summon some armor stand(depends on amount of dyes) with tag which equals dye name. Examples:

/summon armor_stand x y z {Tags:["red","dye"]}

/summon armor_stand x y z {Tags:["blue","dye"]}

/summon armor_stand x y z {Tags:["green","dye"]}

etc

Than checking a command block under player: Repeating chain:

/scoreboard players tag @a[tag=rdye] remove rdye

/execute @a ~ ~ ~ detect ~ ~-1 ~ command_block -1 /scoreboard players
tag @a[tag=!rdye] add rdye

/execute @a[tag=rdye] ~ ~ ~ /exeute @r[type=armor_stand,tag=dye] ~ ~ ~
/setblock ~ ~-1 ~ stone

Next duplicate this command for each armor stand with tag

/execute @e[tag=red] ~ ~ ~ decetc ~ ~ ~ stone 0 /give @a[tag=rdye] dye
1 1
/execute @e[tag=blue] ~ ~ ~ decetc ~ ~ ~ stone 0 /give @a[tag=rdye]
dye 1 4
/execute @e[tag=green] ~ ~ ~ decetc ~ ~ ~ stone 0 /give @a[tag=rdye]
dye 1 2

etc

/execute @e[tag=dye] ~ ~ ~ /setblock ~ ~-1 ~ dirt

it can be any block (instead of dirt).

I know that there are many methods, but I think it is more easy than methods with droppers or item rotation etc.