I am trying to get a random 16 digit number in Lua. What I have written isn't working out for me when logically it should. How does math.random work with exponents?
This is what I keep getting.
> return math.random(10^15, 10^16)
> -1637272360
If you want to have a 16 digit number, try generating them this way:
local fmt = "%d%07d%08d"
local random = math.random
local num = fmt:format(random(1, 9), random(0, 10^7), random(0, 10^8))
and then keep the variable num in string type. As a number, it converts the values to exponential form(because of the very large; in your case > 10^14; exponential value) or otherwise, you can store them as a(n) Hex string?
Related
I have a 4 byte hexadecimal value that I have a script to print out, But I want to now take that value then subtract the value C8 from it 37 times and save them as different variables each time, But the problem is I don't know how to do hexadecimal calculations in lua, If anyone can link me to any documentation on how to do this then that would be much appreciated.
You can make a hexadecimal literal in Lua by prefixing it with 0x, as stated in the reference manual. I found this by googling "lua hex"; such searches usually get good results.
"Hexadecimal numbers" aren't anything special, hexadecimal is just a way to represent numbers, same as decimal or binary. You can do 1000-0xC8 and you'll get the decimal number 800.
Code to convert:
function convertHex()
local decValue = readInteger(0x123456);
hexValue = decValue
end
function hexSubtract()
for i = 1,37 do
local value = 0xC8
hexValue = hexValue - 0xC8
result = hexValue
if i == 37 then
print(result) --Prints dec value
print(string.format('%X',result)); --Prints hex value
end
end
end
Replace 0x123456 with your address, use those functions like this convertHex(),hexSubtract()
I've got the following code in lua:
array = {}
for i=1, 30 do
mem = tostring(0x29A300 + (i * 0x11CC))
array[i] = "Ref Mem" .. i .. ": " .. mem
end
But when I do the hexadecimal addition to store in mem, I get a decimal value out.
Is there a way to keep the hex formatting that I used to do the calculation?
Also is there a way for me to remove the 0x in the string?
I've tried using tonumber but that gives me an error.
There hasn't been anything else I've tried as I'm completely new to lua.
Sorry about the bad title, I couldn't think of anything better. So I was making a function to shorten numbers(1000 to 1k) and here it is.
local letters = {
"K",
"M", --I'm too lazy to put more
"B",
"QD",
"QN",
}
local nums = {}
for q=1,#letters do
local dig = (q*3)+1
local letter = 1*(10^(dig-1))
table.insert(nums,#nums+1,letter)
end
function shorten(num)
local len = tostring(num):len()
print(len)
if len>=4 then
for q=1,#letters do
local dig = (q*3)+1 --digits the letter is
if len <= dig+2 then
local toDo = math.floor(num/nums[q])
print(nums[q])
local newNum = toDo..letters[q]
return newNum
end
end
end
end
print(shorten(178900000000000))
And this prints.
10 --the length, and the real length of it is 15
1000000000 --the one to divide it
178900B --shortened num
I take one zero off of the print(shorten()) and it works fine. And I'm assuming the numbers are too big, or maybe there's a problem with the code. Thank you for reading this.
tostring gives the human-readable string representation, and for a big number like in your example, it uses scientific notation:
print(tostring(178900000000000))
In Lua 5.2 or lower, the result if 1.789e+14. In Lua 5.3, because of the newly introduced integer subtype, the result is 178900000000000 as expected, but it would still be broken for even bigger integers.
I'm receiveing in a lua script a hash from redis with numbers in a string format.
1) "30"
2) "30.7"
3) "12.7"
4) "15.7"
5) "20.7"
6) "19.7"
7) "20.5"
8) "21.5"
9) "22.3"
10) "30.7"
I know that Lua does not differentiate between floating point numbers and integers.
This is the script:
local sum = "0.0"
local matches = redis.call('KEYS', 'sdid:*')
for _,key in ipairs(matches) do
local val = redis.call('HGET', key ,'data')
sum = sum + val
end
return sum
I receive the result in integer format ((integer) 224
) how could I receive de result in a real format string "224.5" ?
In arithmetic operations, Lua automatically converts strings containing numbers to numbers.
So, just add the strings with a+b+c and you'll get the number 6.6.
If you want the result with 1 decimal place even if its an integer, use string.format("%.1f",sum).
I am trying to return very long integer number but my result returns as
"7.6561197971049e+016".
How do I make it return 76561197971049296 ?
local id64 = 76561197960265728
Z = string.match("STEAM_0:0:5391784", 'STEAM_%d+:%d+:(%d+)')
Y = string.match("STEAM_0:0:5391784", 'STEAM_%d+:(%d+):%d+')
--For 64-bit systems
--Let X, Y and Z constants be defined by the SteamID: STEAM_X:Y:Z.
--Let V be SteamID64 identifier of the account type (0x0110000100000000 in hexadecimal format).
--Using the formula W=Z*2+V+Y
if Z == nil then
return "none"
else
return Z*2+id64+Y
end
I installed lbc arbitrary precision now with this code
return bc.add(bc.number(id64),bc.number(2)):tostring()
it returns 70000000000000002 but if I delete 3 digits from id64 it displays correctly.
How can I get correct result without deleting the digits?
You need to use strings for long numbers. Otherwise, the Lua lexer converts them to doubles and loses precision in this case. Here is code using my lbc:
local bc=require"bc"
local id64=bc.number"76561197960265728"
local Y,Z=string.match("STEAM_0:0:5391784",'STEAM_%d+:(%d+):(%d+)')
if Z == nil then
return "none"
else
return (Z*2+id64+Y):tostring()
end
check out this library for arbitrary precision arithmetics. this so post might be of interest to you as well.
Assuming your implementation of Lua supports that many significant digits in the number type, your return statement is returning that result.
You're probably seeing exponential notation when you convert the number to a string or printing it. You can use the string.format function to control the conversion:
assert( "76561197971049296" == string.format("%0.17g", 76561197971049296))
If number is an IEEE-754 double, then it doesn't work. You do have to know how your Lua is implemented and keep in mind the the technical limitations.
If you have luajit installed, you can do this:
local ffi = require("ffi")
steamid64 = tostring(ffi.new("uint64_t", 76561197960265728) + ffi.new("uint64_t", tonumber(accountid)))
steamid64 = string.sub(steamid64, 1, -4) -- to remove 'ULL at the end'
Hope it helps.