How do I repeat a script for a random number of times?

I want to know if you can repeat a part of a script a random number of times instead of a set number of times, for a game I am making.

Heres the script I am wanting to repeat for a random number of times:

local candy = r.Candy:GetChildren()
		local randomCandy = candy[math.random(1, #candy)]:Clone()
		randomCandy.Parent = workspace
		randomCandy.Position = script.Parent.CandySpawn.Position

like this:

local maxt = math.random(50) -- max random amount of times
local times = 0

repeat
	times += 1
	local candy = r.Candy:GetChildren()
	local rancandy = candy[math.random(#candy)]:Clone()
	rancandy.Parent = workspace
	rancandy.Position = script.Parent.CandySpawn.Position
until times == maxt

Thank you, it works perfectly.