duancuan7057 2013-11-14 16:01
浏览 40

Lua vs PHP随机数[关闭]

I have a lua script that encrypts a string and sends it via http to a PHP script. Problem is, the encryption process uses Lua's math.randomseed and math.random. When trying to decrypt the string in PHP, mt_srand and mt_rand produce different numbers than Lua did.

How can I get php to generate numbers like Lua?

-- Edit

Ok, so in my (very simple) encryption I'm using a key to generate a seed. That seed lets me get back the same "random number" each time.

So if my key produces a seed of say, 80 and I use this in Lua...

math.randomseed(80)
local randomNumber = math.random(1, 20)
// randomNumber = 3

When trying to decrypt in PHP, I'll use the same seed but I get a different output.

mt_srand(80);
$randomNumber = mt_rand(1, 20);
// $randomNumber = 10

I need to figure out a way to get back the same number so that I'm able to decrypt the string.

  • 写回答

2条回答 默认 最新

  • dongmou1964 2013-11-14 17:09
    关注

    You need a random-number-generator which use the same algorithm on php and lua.

    Either you find an random-number-generator for Lua and one for PHP which has the same implementation, or you must programm your own one. Moreover, using of system intern generators are also bad, because they can change everytime, and then your script also no longer works.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器