Jump to content

random math


Recommended Posts

Im trying to figure out how to find a random group of 10 numbers in a row out of 100. could be 1-10 or 2-11 ect.

function checkBar()
    local check1 = guiScrollBarGetScrollPosition ( bar )
	local check2 = math.random (10,100)
	if check2 == check1 then 
        guiSetVisible(Window,false)
	    showCursor(false)
        removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent (TODO)
	end
end
bindKey ( "E", "down", checkBar )

 

Link to comment

You want to get 10 random numbers from specified value? If i understood you correctly then you can do this.

local randomNumbers = {} -- create table.

for i=1,10 do -- loop through function beloew 10 times.
	table.insert(randomNumbers,math.random(100)) -- insert them to table
end

-- now when you need those random numbers you can just loop through them like this.
for _,number in pairs(randomNumbers) do
	iprint(number) -- this will show all numbers in '/debugscript 3' menu.
end

-- if you dont need anymore those 10 random numbers and want to clear them to insert other 10 random numbers just do this
randomNumbers = {} -- this will empty the table
Link to comment

That still does not give me 10 numbers in a row

function checkBar()
    local check1 = guiScrollBarGetScrollPosition ( bar )
	a = {0,1,2,3,4,5,6,7,8,9}
	--b = {1,2,3,4,5,6,7,8,9,10} here is what i mean by ect i need 10 numbers in a row 
	local check2 = a[ math.random (#a)]
	if check2 == check1 then 
    guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent ()
	end
end
bindKey ( "E", "down", checkBar )

here check2 works for table "a" all numbers will return true --or not i thought it was working guess i was dreaming

 im basicly trying to section off 100 in random groups of 10 to make a guessing game out of it.

rather than having 10 random numbers, i need random groups of 10 numbers

Edited by greentumbleweed
Link to comment
1 hour ago, greentumbleweed said:

That still does not give me 10 numbers in a row


function checkBar()
    local check1 = guiScrollBarGetScrollPosition ( bar )
	a = {0,1,2,3,4,5,6,7,8,9}
	--b = {1,2,3,4,5,6,7,8,9,10} here is what i mean by ect i need 10 numbers in a row 
	local check2 = a[ math.random (#a)]
	if check2 == check1 then 
    guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent ()
	end
end
bindKey ( "E", "down", checkBar )

here check2 works for table "a" all numbers will return true --or not i thought it was working guess i was dreaming

 im basicly trying to section off 100 in random groups of 10 to make a guessing game out of it.

rather than having 10 random numbers, i need random groups of 10 numbers

ah i think i understood you, here this should do the trick. Haven't tested it but should work.

function generateRandomGroupNumbers()
	local randomNumbers = {} -- create temporary table to store numbers
	local startingNumber = math.random(90) -- get random starting position
	for i=startingNumber,10 do -- add 10 numbers from starting position
		table.insert(randomNumbers,i) -- insert them to table
	end
	return randomNumbers; -- returns table with numbers when numbers have been generated
end

-- this is an example how to use function
local numbers = generateRandomGroupNumbers();

-- now lets loop through table (You should use ipairs for this if you want them to be exactly in correct order)
for i,number in ipairs(numbers) do
	iprint(number)
end

 

Link to comment
11 hours ago, greentumbleweed said:

not sure why but its not printing the numbers

 

maybe try with pairs instead, sometimes it just doesn't work with ipairs happend to me before but in theory it should anyway print numbers out in correct order, but if they don't then you gonna need use table.sort but i'm pretty sure it will work any way.

for i,number in pairs(numbers) do
	iprint(number)
end

 

Link to comment

i finally figured out the problem 

function generateRandomGroupNumbers()
	local randomNumbers = {} -- create temporary table to store numbers
	local startingNumber = math.random(90) -- get random starting position
	for i=startingNumber,10 do -- add 10 numbers from starting position
		table.insert(randomNumbers,i) -- insert them to table
	end
	return randomNumbers; -- returns table with numbers when numbers have been generated
end

^i understand that for... is a loop and the 10 should mean loop 10x but it will not print 

so the startingNumber is almost always higher than 10 meaning its giving (90 or less ,10) for i

 when i change the values to

local startingNumber = math.random(10) -- get random starting position
	for i=startingNumber,100 do -- add 10 numbers from starting position

 it prints (10 or less)-100 

i dont get why it wont print with a lower value of loops makes no sense to me

 

and i fully understand what you wrote it should do exactly what you intended it just doesnt print:S

Edited by greentumbleweed
Link to comment
1 hour ago, greentumbleweed said:

i finally figured out the problem 


function generateRandomGroupNumbers()
	local randomNumbers = {} -- create temporary table to store numbers
	local startingNumber = math.random(90) -- get random starting position
	for i=startingNumber,10 do -- add 10 numbers from starting position
		table.insert(randomNumbers,i) -- insert them to table
	end
	return randomNumbers; -- returns table with numbers when numbers have been generated
end

^i understand that for... is a loop and the 10 should mean loop 10x but it will not print 

so the startingNumber is almost always higher than 10 meaning its giving (90 or less ,10) for i

 when i change the values to


local startingNumber = math.random(10) -- get random starting position
	for i=startingNumber,100 do -- add 10 numbers from starting position

 it prints (10 or less)-100 

i dont get why it wont print with a lower value of loops makes no sense to me

 

and i fully understand what you wrote it should do exactly what you intended it just doesnt print:S

ah that's 'cause startingPosition is more than 10. I didn't think this would affect that 'cause never used it in such way but here is working code. :)

function generateRandomGroupNumbers()
	local randomNumbers = {} -- create temporary table to store numbers
	local startingNumber = math.random(90) -- get random starting position
	for i=startingNumber,startingNumber+10 do -- add 10 numbers from starting position
		table.insert(randomNumbers,i)
	end
	return randomNumbers; -- returns table with numbers when numbers have been generated
end

addCommandHandler("do",function(source,cmd)
	local table = generateRandomGroupNumbers();
	iprint(table)
end)

 

Link to comment
function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	if randomNumbers == check1 then 
    guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent ()
	end
end
bindKey ( "E", "down", checkBar )

how can i call the numbers from the table at 

if  randomNumbers  ==  check1  then

 

im a noob with table functions xD

Edited by greentumbleweed
Link to comment
11 hours ago, greentumbleweed said:

function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	if randomNumbers == check1 then 
    guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent ()
	end
end
bindKey ( "E", "down", checkBar )

how can i call the numbers from the table at 

if  randomNumbers  ==  check1  then

 

im a noob with table functions xD

you have to loop through table like this

for _,v in pairs(tableVariableHere) do
	-- this code here will execute as many times as items are in table so you just need to add check variable if v == check1 then once it found it and then we can return true or any variable back or even set any other variable if you need
	if (v == check1) then
    	-- this code will be executed once check1 matches one of numbers that is in table
    end
end

 

Link to comment
function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	for _,v in ipairs(randomNumbers) do
	if (v == check1) then
    	guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate) 
	--triggerServerEvent ()
    end
end
local randomNumbers = {} 
	local startingNumber = math.random(90) 
	for i=startingNumber,startingNumber+10 do 
		table.insert(randomNumbers,i)
	end
	return randomNumbers; 
end
bindKey ( "E", "down", checkBar )

i hope i did this right im not getting any errors in debug but its not exactly working not sure if its because v = multiple answers and check1 is one sigle answer 

Link to comment
9 hours ago, Fist said:

you have to loop through table like this


for _,v in pairs(tableVariableHere) do
	-- this code here will execute as many times as items are in table so you just need to add check variable if v == check1 then once it found it and then we can return true or any variable back or even set any other variable if you need
	if (v == check1) then
    	-- this code will be executed once check1 matches one of numbers that is in table
    end
end

 

function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	local table = generateRandomGroupNumbers();
	for _,v in ipairs(table) do
	if (v == check1) then
    	guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate)
	--triggerServerEvent ()
    end
end
end
bindKey ( "E", "down", checkBar )

^ this works but the only problem remaining is if i spam E it will keep changing the numbers

i need the numbers to stay the same O.o

 

Link to comment
4 minutes ago, greentumbleweed said:

function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	local table = generateRandomGroupNumbers();
	for _,v in ipairs(table) do
	if (v == check1) then
    	guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate)
	--triggerServerEvent ()
    end
end
end
bindKey ( "E", "down", checkBar )

^ this works but the only problem remaining is if i spam E it will keep changing the numbers

i need the numbers to stay the same O.o

 

use timer to avoid spamming

Link to comment
4 minutes ago, #BrosS said:

use timer to avoid spamming

I dont think thats the problem here, because im calling it with the function. So.... every time i press E the numbers will be different, i just need to grab the numbers from the table instead of calling the function. I'm just not sure how.

 

This is for a lockpicking system kind of like dying light.

 

Edited by greentumbleweed
Link to comment
33 minutes ago, greentumbleweed said:

I dont think thats the problem here, because im calling it with the function. So.... every time i press E the numbers will be different, i just need to grab the numbers from the table instead of calling the function. I'm just not sure how.

 

This is for a lockpicking system kind of like dying light.

 

Everytime you press it will call this function generateRandomGroupNumbers() and everytime it calls it, it will delete previous numbers and get new ones. So solution for this is that you have to call it only once.

local table = generateRandomGroupNumbers()
function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	for _,v in ipairs(table) do
	if (v == check1) then
    	guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate)
	--triggerServerEvent ()
    end
end
end
bindKey ( "E", "down", checkBar )

 

Edited by Fist
Link to comment
15 minutes ago, Fist said:

Everytime you press it will call this function generateRandomGroupNumbers() and everytime it calls it, it will delete previous numbers and get new ones. So solution for this is that you have to call it only once.


local table = generateRandomGroupNumbers()
function checkBar(source)
    local check1 = guiScrollBarGetScrollPosition ( bar )
	for _,v in ipairs(table) do
	if (v == check1) then
    	guiSetVisible(Window,false)
	showCursor(false)
    removeEventHandler("onClientRender", getRootElement(), openLockedCrate)
	--triggerServerEvent ()
    end
end
end
bindKey ( "E", "down", checkBar )

 

debugscript: attempt to call global ' generateRandomGroupNumbers'(a nill value)

 

Link to comment
1 minute ago, greentumbleweed said:

debugscript: attempt to call global ' generateRandomGroupNumbers'(a nill value)

 

you have to put that generateRandomGroupNumbers function what i wrote above "local table = generateRandomNumbers()"

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...