在 Lua 脚本中生成随机数,Load Impact。

我正在尝试在 Lua 中创建一个随机数生成器。我发现我可以使用 math.random (1,100) 来随机获得 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},
    })
点赞
用户3924687
用户3924687

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"}

同样的逻辑也可以应用到你的代码中:

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 = "{%q}"
print(string.format(data,ran))
--{"59"}

%q 格式化符会将你输入的任何内容,并安全地用引号括起来。

同样的逻辑也可以应用到你的 Http 数据中。

2015-09-13 18:31:48
用户4102899
用户4102899

下面是代码片段的修正版:

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的东西。不确定它是否应该引用变量。如果是,它也需要使用..运算符连接到结果字符串中(就像随机数变量一样)。

2015-09-16 07:59:17