Getting different numbers with Random.new()

  1. **What do you want to achieve?
    I am trying to get different numbers everytime using Random:NextInteger()

  2. **What is the issue?
    Every now and then I get the same number, which is something I don’t want at all in my game.

Is there any way to get different numbers everytime?

2 Likes

This is an example:
Error

I don’t want it to be the same

1 Like

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.

if there is more questions im here.

2 Likes

i’ll try it right now. I’ll tell you if I get it to work

1 Like

i dont get what you mean but alr

1 Like

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

1 Like

Yeah thats why I deleted my post I was confused about what he wanted but math.random sounds like it would be fine for what hes needing.

1 Like

i already have a way of getting random numbers. What i want to do is to not get the same number twice or more times

1 Like

make sure to set blacklist to {} so you dont break the system if this has some kind of a round system.

1 Like

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.

for i = 1, 15 do
    print(math.random(1,15));
end;

there is a huge chance to get doubled actually.

image

2 Likes

yeah no you’re right I completely misunderstood what he was wanting my bad

I found a way to do it. I had to twist this a bit but it’s still using Blacklisting. Thanks!

Quotes

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.

1 Like

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.

2 Likes

You seem to have misunderstood me, and what youre saying is easily disprovable, and incorrect.

I think its funny how you say I dont know how tables work when you clearly dont know yourself.

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.

2 Likes

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