Executing Probabilities All At Once + Extra Steps

My first Forum was answered but I had to scratch that idea because; 1. there will be over 16 million combinations and 2. The RNG will be wrong as well. Right now, what I have is this:

local cubeConfig = {
	{ Chance = 1/1, Color = Color3.fromRGB(255,255,255), Name = "White" },
	{ Chance = 1/13, Color = Color3.fromRGB(30,15,15), Name = "Bighorn Sheep" },
	{ Chance = 1/13, Color = Color3.fromRGB(15,30,15), Name = "Nightmare" },
	{ Chance = 1/13, Color = Color3.fromRGB(15,15,30), Name = "Tristesse" },
	{ Chance = 1/13, Color = Color3.fromRGB(45,15,0), Name = "Chocolate Melange" },
	{ Chance = 1/13, Color = Color3.fromRGB(45,0,15), Name = "Ilvaite Black" },
	{ Chance = 1/14, Color = Color3.fromRGB(15,15,15), Name = "Chaos Black" },
	{ Chance = 1/14, Color = Color3.fromRGB(30,15,0), Name = "Super Black" },
	{ Chance = 1/14, Color = Color3.fromRGB(30,0,15), Name = "Belladonna" },
	{ Chance = 1/14, Color = Color3.fromRGB(15,30,0), Name = "Yuzu Soy" },
	{ Chance = 1/14, Color = Color3.fromRGB(0,30,15), Name = "Clock Chimes Thirteen" },
	{ Chance = 1/14, Color = Color3.fromRGB(15,0,30), Name = "Election Night" },
	{ Chance = 1/14, Color = Color3.fromRGB(0,15,30), Name = "Kuretake Black Manga" },
	{ Chance = 1/14, Color = Color3.fromRGB(45,0,0), Name = "Sepia Black" },
	{ Chance = 1/14, Color = Color3.fromRGB(0,45,0), Name = "PCB Green" },
	{ Chance = 1/14, Color = Color3.fromRGB(0,0,45), Name = "Illicit Darkness" },
	{ Chance = 1/15, Color = Color3.fromRGB(15,15,0), Name = "Chinese Black"},
	{ Chance = 1/15, Color = Color3.fromRGB(15,0,15), Name = "Glossy Black" },
	{ Chance = 1/15, Color = Color3.fromRGB(0,15,15), Name = "Black Glaze" },
	{ Chance = 1/15, Color = Color3.fromRGB(30,0,0), Name = "Dwarf Fortress" },
	{ Chance = 1/15, Color = Color3.fromRGB(0,30,0), Name = "Rainforest Nights" },
	{ Chance = 1/15, Color = Color3.fromRGB(0,0,30), Name = "Hadopelagic Water" },
	{ Chance = 1/16, Color = Color3.fromRGB(15,0,0), Name = "Dark Matter"},
	{ Chance = 1/16, Color = Color3.fromRGB(0,15,0), Name = "Cosmic Bit Flip" }, 
	{ Chance = 1/16, Color = Color3.fromRGB(0,0,15), Name = "Benthic Black" },
	{ Chance = 1/17, Color = Color3.fromRGB(0,0,0), Name = "Black" },
}

local sum = 0
for _, cube in pairs(cubeConfig) do
	sum += cube.Chance
end

local draw = math.random() * sum
for _, cube in pairs(cubeConfig) do
	if draw <= cube.Chance then 
		print(cube.Color)
		break
	end

	draw -= cube.Chance
end

-- Schedule the cube to despawn after 60 seconds
game:GetService("Debris"):AddItem(cube, 60)
end
wait()
end

I decreased the amount of colors, and started making a list of putting the color probabilities from most common (white) to the most rarest (black) by increments of 15 RGB light. Not only do I want want to know how to execute all probabilities at once, I also want to know how to make part of the script select the lowest probability that was able to succeed and print it on the basic cube. (If multiple colors of the same probability succeed, the script will randomly choose a color from that set of successful colors.)

I tried using the Roblox assistant. I’m staring to realize that it’s not that reliable for calculations…

1 Like

You could try doing something like this:

local CubeConfig = {
		{ Chance = 1/1, Color = Color3.fromRGB(255,255,255), Name = "White" },
		{ Chance = 1/13, Color = Color3.fromRGB(30,15,15), Name = "Bighorn Sheep" },
		{ Chance = 1/13, Color = Color3.fromRGB(15,30,15), Name = "Nightmare" },
		{ Chance = 1/13, Color = Color3.fromRGB(15,15,30), Name = "Tristesse" },
		{ Chance = 1/13, Color = Color3.fromRGB(45,15,0), Name = "Chocolate Melange" },
		{ Chance = 1/13, Color = Color3.fromRGB(45,0,15), Name = "Ilvaite Black" },
		{ Chance = 1/14, Color = Color3.fromRGB(15,15,15), Name = "Chaos Black" },
		{ Chance = 1/14, Color = Color3.fromRGB(30,15,0), Name = "Super Black" },
		{ Chance = 1/14, Color = Color3.fromRGB(30,0,15), Name = "Belladonna" },
		{ Chance = 1/14, Color = Color3.fromRGB(15,30,0), Name = "Yuzu Soy" },
		{ Chance = 1/14, Color = Color3.fromRGB(0,30,15), Name = "Clock Chimes Thirteen" },
		{ Chance = 1/14, Color = Color3.fromRGB(15,0,30), Name = "Election Night" },
		{ Chance = 1/14, Color = Color3.fromRGB(0,15,30), Name = "Kuretake Black Manga" },
		{ Chance = 1/14, Color = Color3.fromRGB(45,0,0), Name = "Sepia Black" },
		{ Chance = 1/14, Color = Color3.fromRGB(0,45,0), Name = "PCB Green" },
		{ Chance = 1/14, Color = Color3.fromRGB(0,0,45), Name = "Illicit Darkness" },
		{ Chance = 1/15, Color = Color3.fromRGB(15,15,0), Name = "Chinese Black"},
		{ Chance = 1/15, Color = Color3.fromRGB(15,0,15), Name = "Glossy Black" },
		{ Chance = 1/15, Color = Color3.fromRGB(0,15,15), Name = "Black Glaze" },
		{ Chance = 1/15, Color = Color3.fromRGB(30,0,0), Name = "Dwarf Fortress" },
		{ Chance = 1/15, Color = Color3.fromRGB(0,30,0), Name = "Rainforest Nights" },
		{ Chance = 1/15, Color = Color3.fromRGB(0,0,30), Name = "Hadopelagic Water" },
		{ Chance = 1/16, Color = Color3.fromRGB(15,0,0), Name = "Dark Matter"},
		{ Chance = 1/16, Color = Color3.fromRGB(0,15,0), Name = "Cosmic Bit Flip" }, 
		{ Chance = 1/16, Color = Color3.fromRGB(0,0,15), Name = "Benthic Black" },
		{ Chance = 1/17, Color = Color3.fromRGB(0,0,0), Name = "Black" },
	}

	local Sum = 0

	for _, Cube in pairs(CubeConfig) do
		Sum += Cube.Chance
	end
	
	local CubesToRoll = {}
	
	local RNG = math.random() * Sum
	for _, Cube in pairs(CubeConfig) do
		if RNG <= Cube.Chance then 
			
			for _, Cube2 in pairs(CubeConfig) do
				--Add all cubes that have the "winning" chance to a table to determine which will win
				if Cube2.Chance == Cube.Chance then
					table.insert(CubesToRoll, Cube2)
				end
			end
			
			break
		end
		
		RNG -= Cube.Chance
	end
	
	-- Roll through all potential winners
	local WinningCube = CubesToRoll[math.random(#CubesToRoll)]
	
	print(WinningCube, WinningCube.Name)
	workspace.Baseplate.Color = WinningCube.Color

Basically, it just rerolls (with an even chance) among all the colors that have the same chance as the originally selected color.

I put it together and it was extremely close to what I wanted, but what I encounter was that only the first cube dropped had any color, every cube after that was the default as the first cube kept changing colors.

while true do
	-- Check if it's time to spawn a new cube
	if tick() - lastCubeSpawnTime >= cubeSpawnInterval then
		lastCubeSpawnTime = tick()
		local cube = Instance.new("Part")
		cube.Name = "BasicCube"
		cube.Size = Vector3.new(4, 4, 4)
		cube.Position = Vector3.new(math.random(-250, 250), 1000, math.random(-250, 250))
		cube.Anchored = false
		cube.Parent = game.Workspace
		table.insert(cubes, cube)

		local Sum = 0

		for _, Cube in pairs(CubeConfig) do
			Sum += Cube.Chance
		end

		local CubesToRoll = {}

		local RNG = math.random() * Sum
		for _, Cube in pairs(CubeConfig) do
			if RNG <= Cube.Chance then 

				for _, Cube2 in pairs(CubeConfig) do
					--Add all cubes that have the "winning" chance to a table to determine which will win
					if Cube2.Chance == Cube.Chance then
						table.insert(CubesToRoll, Cube2)
					end
				end

				break
			end

			RNG -= Cube.Chance
		end
		
		-- Roll through all potential winners
		local WinningCube = CubesToRoll[math.random(#CubesToRoll)]
		
		print(WinningCube, WinningCube.Name)
		workspace.BasicCube.Color = WinningCube.Color


	end
	wait()
		
end

Might need to save this code as a color changing rarity. Lol

This would be because you are changing workspace.BasicCube not cube!

Try replacing:
workspace.BasicCube.Color = WinningCube.Color
with:
cube.Color = WinningCube.Color

It still seems like white is a minority, a dark color should at least spawn every 13 whites if I’m not mistaken.

Im not sure what you mean by that, but I dont think its related to my solution. I think its related to your rolling system, I dont fully understand it so I cant help you further :frowning:

You could also try using a different rolling system!

Like im sure there’s some minor probability flaw or something but this is what I use in all my projects, It works too well and not used enough for it to be perfect if you know what I mean. (yap)

This is it:

if math.random(1, CHANCE) == 1 then
	-- Continue with this object
end
Example
local Fruits = {
	{Chance = 1, Name = "Apple"},
	{Chance = 3, Name = "Orange"},
	{Chance = 5, Name = "Pear"},
}

for _, Fruit in Fruits do
	if math.random(1, Fruit.Chance) == 1 then
		print("This fruit won:", Fruit.Name)
		break
	end
end

You would have it something like this:

local CubeConfig = {
	{ Chance = 1, Color = Color3.fromRGB(255,255,255), Name = "White" },
	{ Chance = 13, Color = Color3.fromRGB(30,15,15), Name = "Bighorn Sheep" },
	{ Chance = 13, Color = Color3.fromRGB(15,30,15), Name = "Nightmare" },
	{ Chance = 13, Color = Color3.fromRGB(15,15,30), Name = "Tristesse" },
	{ Chance = 13, Color = Color3.fromRGB(45,15,0), Name = "Chocolate Melange" },
	{ Chance = 13, Color = Color3.fromRGB(45,0,15), Name = "Ilvaite Black" },
	{ Chance = 14, Color = Color3.fromRGB(15,15,15), Name = "Chaos Black" },
	{ Chance = 14, Color = Color3.fromRGB(30,15,0), Name = "Super Black" },
	{ Chance = 14, Color = Color3.fromRGB(30,0,15), Name = "Belladonna" },
	{ Chance = 14, Color = Color3.fromRGB(15,30,0), Name = "Yuzu Soy" },
	{ Chance = 14, Color = Color3.fromRGB(0,30,15), Name = "Clock Chimes Thirteen" },
	{ Chance = 14, Color = Color3.fromRGB(15,0,30), Name = "Election Night" },
	{ Chance = 14, Color = Color3.fromRGB(0,15,30), Name = "Kuretake Black Manga" },
	{ Chance = 14, Color = Color3.fromRGB(45,0,0), Name = "Sepia Black" },
	{ Chance = 14, Color = Color3.fromRGB(0,45,0), Name = "PCB Green" },
	{ Chance = 14, Color = Color3.fromRGB(0,0,45), Name = "Illicit Darkness" },
	{ Chance = 15, Color = Color3.fromRGB(15,15,0), Name = "Chinese Black"},
	{ Chance = 15, Color = Color3.fromRGB(15,0,15), Name = "Glossy Black" },
	{ Chance = 15, Color = Color3.fromRGB(0,15,15), Name = "Black Glaze" },
	{ Chance = 15, Color = Color3.fromRGB(30,0,0), Name = "Dwarf Fortress" },
	{ Chance = 15, Color = Color3.fromRGB(0,30,0), Name = "Rainforest Nights" },
	{ Chance = 15, Color = Color3.fromRGB(0,0,30), Name = "Hadopelagic Water" },
	{ Chance = 16, Color = Color3.fromRGB(15,0,0), Name = "Dark Matter"},
	{ Chance = 16, Color = Color3.fromRGB(0,15,0), Name = "Cosmic Bit Flip" }, 
	{ Chance = 16, Color = Color3.fromRGB(0,0,15), Name = "Benthic Black" },
	{ Chance = 17, Color = Color3.fromRGB(0,0,0), Name = "Black" },
}

table.sort(CubeConfig, function(a, b)
	return a.Chance < b.Chance
end)

local CubesToRoll = {}

for _, Cube in pairs(CubeConfig) do
	if math.random(1,  Cube.Chance) == 1 then 

		for _, Cube2 in pairs(CubeConfig) do
			--Add all cubes that have the "winning" chance to a table to determine which will win
			if Cube2.Chance == Cube.Chance then
				table.insert(CubesToRoll, Cube2)
			end
		end

		break
	end
end

-- Roll through all potential winners
local WinningCube = CubesToRoll[math.random(#CubesToRoll)]

print(WinningCube, WinningCube.Name)
workspace.Baseplate.Color = WinningCube.Color

Hope this helped you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.