lua中require随机读取的代码案例

本文介绍了在Lua中通过不同方法实现随机require模块的过程,包括使用if语句随机选择、利用随机数组进行require以及通过随机变量require等三种方法,并分享了作者在本地调试遇到的问题及解决经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚开始我在本地调试出错一直觉得是代码语法错误,最后在android设备调试一切OK,原来是我本地编辑器导致的。

方法一:这是一个笨方法,if随机require

package.path=package.path..";/mnt/sdcard/sghqs/?.lua;"
math.randomseed(tostring(os.time()):reverse():sub(1,6))
weijian=math.random(1,3)
if weijian==1 then
	require "1"
elseif weijian==2 then
	require "2"
elseif weijian==3 then
	require "3"
end 

方法二:require随机数组

package.path=package.path..";/mnt/sdcard/sghqs/test/?.lua;"
weijian={[1]="1",[2]="2",[3]="3",[4]="4",[5]="5"}
function main()
	while true do
		math.randomseed(tostring(os.time()):reverse():sub(1,6))
		require (weijian[math.random(1,5)])
		mSleep(math.random(1000,2000));
		ff();
		mSleep(math.random(1000,2000));
	end 
end
方法三:require随机变量

function main()
	while true do
	package.path=package.path..";/mnt/sdcard/sghqs/test/?.lua;"
	math.randomseed(tostring(os.time()):reverse():sub(1,6))
	weijian=math.random(1,5)
	require (weijian)
	mSleep(math.random(1000,5000));
	ff();
	mSleep(math.random(1000,5000));
	end 
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值