Blacklist numbers. check if the random number you got belongs to the blacklisted numbers. if not then complete and add it to the blacklisted numbers. if yes then get a new number.
if you ask me. i dont work with Random.new but i can help you using the way.
local blacklist = {}
a = function()
local number = Random.new() -- i dont know how it works you do it yourself
for i, v in blacklist do
if number = v then
a()
else
table.insert(blacklist, number)
return number
end;
end;
end;
if you turn the function into a local function it would fail. you cant call the function inside itself if its local. it must be global as you can see.
i used this in brute force. i learnt it myself and i created the way… i dont know it its still working but all you gotta do is organize it and dont copy-paste everything
Well if youre wanting to keep the way you have then blacklisting is the way to go but math.random will guarantee not getting the same numbers over again unless your just really unlucky
my brute force used math.random and it was actually not good… i prefer Random.new if you ask me. also math.random has same numbers its so common actually. if you dont get what i mean make a loop and see what you will get.
Well if you really wanted to get into specifics, Random.new() and math.random() generate random numbers using whats known as psuedorandom generation, which is an algorithm that generates a “random number”, the thing about it is that its not actually random, in fact the number you get is already determined by another number entirely when used, Functionally both Random.new() and math.random() are the exact same, so they arent completely different things, the Only thing I would look out for is their Integer limits.
Code Quote
I dont really understand the purpose of using a for loop to iterate through your Data, it just doesnt seem very efficient, unless you have the number given to an index, it would be much faster to check the index, or by the usage of table.find.
If you dont know, an index would basically be the number of something that holds a value in a table, for example, lets say I named the index bob, and gave it a table value, you see bob is very special, if you dont give an index a name, it will be assigned as a number, but instead of trying to get a value from a number, we can get bob, like so:
local table = {
bob = {} -- bob is the index, {} is the value
}
print(table["bob"]) -- "bob" will give us a table in return, if the index doesnt exist, it will return nil
Now why is this important? Its important because it means we can give our own index names, we are able to say they are a specific number, or a string, so for the relevancy, you can create an index using a random, and check if a index exists:
if table["Hello"] ~= nil then -- if the index is not equal to nil (it exists)
print("index exists")
end
-- math.random example:
local number = math.random(1, 15)
if table[number] then -- if the index is not equal to nil (it exists)
warn("Already in table")
else -- if the index is equal to nil (does not exists)
print("Not in table")
table[number] = value -- the number is now an index of the table
end
So Instead of iterating through a table, you can give that random number an index, and look for the index instead.
you have mixed up indexes with index numbers. when you do 1 = {}, 1 is a value. when you do 2 = {}, 2 is a value. if you do something like
local r = {2 = "bob"}
print(r[2])
that is going to return nil because it is not defined. instead you will have to loop through them and check the value itself. if there is no index but just value. the index is automatically going to turn into an index with the next index number.
roblox made this so table.find() is the best way to fix it. your way is going to actually print nil.
second point, i told him to learn one of the ways to prevent using the same number. i prefer you learning more about scripting specially in tables. hope to see you again soon when youre better at it.
Actually, it’s a syntax error and won’t run at all. If you fix the syntax error like so:
local r = {[2] = "bob"}
print(r[2])
it works properly and prints bob.
In that scenario (assuming you fix the aforementioned syntax error) 1 is a key or an index. The value is an empty table {}
table.find is O(n), so for a list of a thousand items it takes a thousand “work”. Dictionaries are O(1) so no matter how big the dictionary is, it only takes one “work”. Ergo, table.find is actually not the best way to fix it since we can just use dictionaries like @DasKairo suggested.
seems like you’re actually right and I made a mistake. ill get better at tables. same for you, and btw the topic has been solved. if you have any additional details say it otherwise you shouldn’t really talk much about this here.
Seems like I actually missunderstood this. I don’t work much with numeric indexes. more like strings is a better way for it when I use them. tostring() helps me so I don’t get any errors while working. If so then I actually made a mistake.
most likely you know what I’m talking about but if not then you can check this:
Thanks for noticing me.
Hope to see you again somewhere.