Random Order Generation Not Working!

So I’m trying to make a function that randomly generates an “order” for the player to fufill but I’m having some issues. Firstly, it always generates 2 items per order, when it should be a 50/50 chance. Secondly, it has a chance to generate 2 of the same item. For example, [Olives, Olives] or [Pepperoni, Pepperoni]. Here’s the code:

local function genOrder()
	local chances = {
		["1"] = 50;
		["2"] = 50
	}
	local rnum = math.random(1,100)
	local tnum
	local weight = 0
	for name, value in chances do
		weight += value
		if rnum <= weight then
			-- Yipee!
			tnum = tonumber(name)
		end
	end
	print(tostring(tnum).." toppings required in this order.")
	local newOrder = {}
	local rnum2
	for count = 1, tnum do
		task.wait()
		local newTop : Model
		repeat 

			newTop = toppings[math.random(1, #toppings)] -- Variable -toppings- is equal to the children of a folder in serverstorage
		until not newOrder.newTop
		table.insert(newOrder, newTop)
		print(tostring(#newOrder).." out of "..tostring(tnum).." toppings loaded in order.")
		if #newOrder >= tnum then 
			print("All toppings have been added to the order!") 
			print(newOrder)
		end
	end
	
	
	return newOrder
		
end
1 Like

Hello, I noticed these issues in your code that were causing the problems mentioned here.

Firstly, the item count selector will always end up being 2 because you never actually break out of the loop after determining a value. This means that the result will always be the last entry in chances, in this case being 2. To fix this you can add:

if rnum <= weight then
	-- Yipee!
	tnum = tonumber(name)
	break -- break out of the loop after selection
end

Secondly, the way you are determining duplicates does not work in this case. By checking not newOrder.newTop, you are checking if the actual index "newTop" exists, which is never set. (If you want to get a dictionary index through a variable’s content, you can do: dictionary[variable]) table.insert() also only inserts via (sequential) number indices, meaning that this method of checking wouldn’t work anyway.
To fix this, I would probably suggest cloning the toppings table (table.clone()) and then removing a random index using table.remove() for every item. table.remove() will return the selected item, and remove it from the table, shifting over all indices. This means that you will never get a duplicate item when getting multiple items in a single iteration. Here’s what it would look like:

local toppingsList = table.clone(toppings)
for count = 1, tnum do
	local newTop : Model = table.remove(toppingsList,math.random(1,#toppingsList))
	table.insert(newOrder, newTop)
	...

Hope this helps!

2 Likes

Thanks! However, I noticed some bugs that still occur:


Whenever it wants to generate 2 orders, it just cancels the generation and moves on to the next order. Because of this, all the orders are singular.

1 Like

To solve the 2 items each time, create a variable called “ranSpawn” or something, and set its value to math.random(1,2). Then, change the start for loop to this:

if ranSpawn == 1 then
    for name, value in chances do
		weight += value
		if rnum <= weight then
			-- Yipee!
			tnum = tonumber(name)
		end
	end
end

just wrap it in a if loop

for the second issue, i beleive you can just look at the first item, and if the outcome for the second item is = to the first item, then just get another random item until outcome 1 ~= outcome 2

Nevermind @BuilderDolphin , I made a mistake in my A/B testing and it turns out the bugs I mentioned in my response were nonexistent! Sorry for worrying you, but it’s fixed! Thanks!

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