Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Random Lua脚本加载影响中的随机数_Random_Lua_Httprequest - Fatal编程技术网

Random Lua脚本加载影响中的随机数

Random Lua脚本加载影响中的随机数,random,lua,httprequest,Random,Lua,Httprequest,我试图在Lua中创建一个随机数生成器。我发现我可以使用math.random(1100)将1到100之间的数字随机化,这就足够了 但我真的不明白如何在脚本中使用随机数作为变量 我试过了,但当然没用 $randomCorr = math.random(1,100); http.request_batch({ {"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"

我试图在Lua中创建一个随机数生成器。我发现我可以使用
math.random(1100)
将1到100之间的数字随机化,这就足够了

但我真的不明白如何在脚本中使用随机数作为变量

我试过了,但当然没用

$randomCorr = math.random(1,100);

http.request_batch({
    {"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"]="application/json;charset=UTF-8"}, data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\"$randomCorr\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}", auto_decompress=true},
    {"GET", "https://store.thestore.com/api/checkout/getproduct?correlationId=$randomCorr", auto_decompress=true},
    })

Lua中,不能使用
$
启动变量名。这就是你的主要问题所在。一旦从代码中删除了
$
,我们就可以很容易地看到如何在Lua中引用变量

randomCorr = math.random(100)
print("The random number:", randomCorr)
randomCorr = math.random(100)
print("New Random Number:", randomCorr)
此外,连接的工作方式与将其隐含到Http数组中的方式不同。您必须在Lua中使用
连接值

请看以下示例:

ran = math.random(100)
data = "{\""..ran.."\"}"
print(data)
--{"14"}
ran = math.random(100)
data = "{%q}"
print(string.format(data,ran))
--{"59"}
您的代码中可以隐含相同的逻辑:

data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\""..randomCorr.."\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}"
或者,可以使用字符串库提供的方法之一格式化中的值

请看以下示例:

ran = math.random(100)
data = "{\""..ran.."\"}"
print(data)
--{"14"}
ran = math.random(100)
data = "{%q}"
print(string.format(data,ran))
--{"59"}
%q
说明符将接受您输入的任何内容,并用引号将其安全地包围起来


同样的逻辑也可以应用于您的Http数据。

Lua中,您不能用
$
启动变量名。这就是你的主要问题所在。一旦从代码中删除了
$
,我们就可以很容易地看到如何在Lua中引用变量

randomCorr = math.random(100)
print("The random number:", randomCorr)
randomCorr = math.random(100)
print("New Random Number:", randomCorr)
此外,连接的工作方式与将其隐含到Http数组中的方式不同。您必须在Lua中使用
连接值

请看以下示例:

ran = math.random(100)
data = "{\""..ran.."\"}"
print(data)
--{"14"}
ran = math.random(100)
data = "{%q}"
print(string.format(data,ran))
--{"59"}
您的代码中可以隐含相同的逻辑:

data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\""..randomCorr.."\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}"
或者,可以使用字符串库提供的方法之一格式化中的值

请看以下示例:

ran = math.random(100)
data = "{\""..ran.."\"}"
print(data)
--{"14"}
ran = math.random(100)
data = "{%q}"
print(string.format(data,ran))
--{"59"}
%q
说明符将接受您输入的任何内容,并用引号将其安全地包围起来


同样的逻辑也可以应用于您的Http数据。

以下是代码片段的更正版本:

local randomCorr = math.random(1,100)

http.request_batch({
{"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"]="application/json;charset=UTF-8"}, data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\"" .. randomCorr .. "\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}", auto_decompress=true},
{"GET", "https://store.thestore.com/api/checkout/getproduct?correlationId=" .. randomCorr, auto_decompress=true},
})

在带引号的字符串中还有一个名为$$hashKey的东西。不确定是否应该引用变量。如果是,还需要使用..将其连接到结果字符串中。。运算符(与randomCorr变量相同)。

以下是代码片段的更正版本:

local randomCorr = math.random(1,100)

http.request_batch({
{"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"]="application/json;charset=UTF-8"}, data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\"" .. randomCorr .. "\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}", auto_decompress=true},
{"GET", "https://store.thestore.com/api/checkout/getproduct?correlationId=" .. randomCorr, auto_decompress=true},
})

在带引号的字符串中还有一个名为$$hashKey的东西。不确定是否应该引用变量。如果是,还需要使用..将其连接到结果字符串中。。运算符(就像使用randomCorr变量一样)。

非常感谢。让我来想想:)现在工作得很有魅力。再次感谢,非常感谢。让我来想想:)现在工作得很有魅力。再次感谢。