Jump to content

Random bank account


Recommended Posts

Hey,

What exactly are you struggling with?
For the 6 digits you generate 6 digits with math.random.
You can store all the used bank numbers in a table and check if the combination matches from one of the records in the table.
If there is a match => regenerate new combination and etc..

Link to comment
8 minutes ago, SpecT said:

Hey,

What exactly are you struggling with?
For the 6 digits you generate 6 digits with math.random.
You can store all the used bank numbers in a table and check if the combination matches from one of the records in the table.
If there is a match => regenerate new combination and etc..

I save all my bank accounts to the database (internal.bd). I don't understand how to make it so that other players can't be given a number that is already recorded in the database

function reg(player)
local account = getPlayerAccount (player)
	local num_1 = math.random(0,9)
	local num_2 = math.random(0,9)
	local num_3 = math.random(0,9)
	local num_4 = math.random(0,9)
	local num_5 = math.random(0,9)
	local num_6 = math.random(0,9)

	
	local num_score = num_1..num_2..num_3..num_4..num_5..num_6
	setAccountData(account, "bank", num_score)
end
addEvent("regbank",true)
addEventHandler("regbank",getRootElement(),reg)

Here is the code

Link to comment

Okay, so something like this:

local bankNumbers = {}

function getAllBankNumbers()
	-- loop through all accounts and get their bank numbers and put them in the table bankNumbers
end

function reg(player)
	local account = getPlayerAccount (player)
	local bankNum
	
	while (in_table(bankNumbers, bankNum)) do -- if the bank number is found in the table keep getting new combination
		bankNum = getRandomBankNum()
	end
	table.insert(bankNumbers, bankNum) -- insert the new bank number

	setAccountData(account, "bank", bankNum)
end
addEvent("regbank",true)
addEventHandler("regbank",getRootElement(),reg)


function getRandomBankNum()
	local bankNum = ""
	for i=1,6 do
  		bankNum = bankNum..tostring(math.random(0,9))
  	end
  	return tonumber(bankNum)
end

-- Util to use to find if a value is already in a table
function in_table(table, value)
    for k, v in pairs(table) do
        if v == value then
            return true, k
        end
    end
    return false
end

Note: You will have to make the getAllBankNumbers function yourself with a for loop through all accounts and put every bank number into that table.
It will need to run on resource start tho!

Edited by SpecT
  • Like 1
Link to comment
1 minute ago, SpecT said:

Okay, so something like this:


local bankNumbers = {}

function getAllBankNumbers()
	-- loop through all accounts and get their bank numbers and put them in the table bankNumbers
end

function reg(player)
	local account = getPlayerAccount (player)
	local bankNum
	
	while (in_table(bankNumbers, bankNum)) do -- if the bank number is found in the table keep getting new combination
		bankNum = getRandomBankNum()
	end
	table.insert(bankNumbers, bankNum) -- insert the new bank number

	setAccountData(account, "bank", bankNum)
end
addEvent("regbank",true)
addEventHandler("regbank",getRootElement(),reg)


function getRandomBankNum()
	local bankNum = ""
	for i=1,6 do
  		bankNum = bankNum..tostring(math.random(0,9))
  	end
  	return tonumber(bankNum)
end

-- Util to use to find if a value is already in a table
function in_table(table, value)
    for k, v in pairs(table) do
        if v == value then
            return true, k
        end
    end
    return false
end

Note: You will have to make the getAllBankNumbers function yourself with a for loop through all accounts and put every bank number into that table.

Okay, thanks

Link to comment

There's a simpler solution: use function getAccountsByData. If no account has the value, the returned table will be empty. So this expression will evaluate to true if the number is already taken:

#getAccountsByData("bank", bank_number) != 0

Apart from being easier, using getAccountsByData has the advantage of working correctly even if new bank numbers get added from outside your script - which may or may not be relevant in your case.

And if you still decide to keep track of them yourself, the problem with the script is that in_table function loops through the whole list of bank numbers whenever you check if a number is registered. That's inefficient because the more registered numbers you have, the slower it gets. Instead of using bankNumbers table as an array (where numbers are stored as values in a sequence), you should use it as a set (where numbers are stored as keys).

-- adding a bank number
bankNumbers[bankNum] = true
-- instead of:
table.insert(bankNumbers, bankNum)

-- checking for a bank number:
while bankNumbers[bankNum] do
-- instead of:
while in_table(bankNumbers, bankNum) do

 

Edited by CrystalMV
Link to comment
On 3/31/2021 at 10:25 PM, CrystalMV said:

There's a simpler solution: use function getAccountsByData. If no account has the value, the returned table will be empty. So this expression will evaluate to true if the number is already taken:


#getAccountsByData("bank", bank_number) != 0

Apart from being easier, using getAccountsByData has the advantage of working correctly even if new bank numbers get added from outside your script - which may or may not be relevant in your case.

And if you still decide to keep track of them yourself, the problem with the script is that in_table function loops through the whole list of bank numbers whenever you check if a number is registered. That's inefficient because the more registered numbers you have, the slower it gets. Instead of using bankNumbers table as an array (where numbers are stored as values in a sequence), you should use it as a set (where numbers are stored as keys).


-- adding a bank number
bankNumbers[bankNum] = true
-- instead of:
table.insert(bankNumbers, bankNum)

-- checking for a bank number:
while bankNumbers[bankNum] do
-- instead of:
while in_table(bankNumbers, bankNum) do

 

I do not understand. Can you please write all the code that should turn out in the end?

Link to comment
  • Scripting Moderators
6 hours ago, Egor_Varaksa said:

I do not understand. Can you please write all the code that should turn out in the end?

We require a effort from you, waiting till someone will do your job it's not a right way, and by doing so you won't learn anything.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...