How to randomize (and remain mutually exclusive)

Ok, so I’m coding a genetics system for Colonies.

function ENT:RandomizeGeneticsOnSpawn() = headcrabgenes[math.random(1,#headcrabgenes)]
	Genetics.Slot1.magn = math.random(0.1,2) = headcrabgenes[math.random(1,#headcrabgenes)]
	Genetics.Slot2.magn = math.random(0.1,2) = headcrabgenes[math.random(1,#headcrabgenes)]
	Genetics.Slot3.magn = math.random(0.1,2) = headcrabgenes[math.random(1,#headcrabgenes)]
	Genetics.Slot4.magn = math.random(0.1,2) = headcrabgenes[math.random(1,#headcrabgenes)]
	Genetics.Slot5.magn = math.random(0.1,2)

Problem is, I need to make sure none of genes are the same. That is, != any others.
Is there any code-efficient way of doing this? I really don’t want to do prohibitively large amounts of code for each slot.

You can make a function to return a function that removes and returns a random variable from a copy of the table so that it can not be returned again.

[lua]function table.Random2(tab)
local t = table.Copy(tab)
return function()
if #t == 0 then
return table.remove(t, math.random(1, #t))


[lua]function ENT:RandomizeGeneticsOnSpawn()
local r = table.Random2(headcrabgenes) = r()
Genetics.Slot1.magn = math.random(0.1,2) = r()
Genetics.Slot2.magn = math.random(0.1,2) = r()
Genetics.Slot3.magn = math.random(0.1,2) = r()
Genetics.Slot4.magn = math.random(0.1,2) = r()
Genetics.Slot5.magn = math.random(0.1,2)



Each slot would then get a random, but different name from the table.

-snip snap-

No it won’t. Look at the code again.


It works, I have tested it. I wouldn’t have posted it if I wasn’t sure that it works.


Also, read through the example. I only call table.Random2 once.

Ah I see now, yeah my bad :confused: