math.random() not working on server ?

8 views
Skip to first unread message

Kevin Hincker

unread,
Feb 11, 2016, 8:40:45 PM2/11/16
to Coronium IO

Chris, I can't get math.random to work on the server. My code:

local randomNum = math.random(300 )
coronium.log(randomNum)

Always produces 239. If I change 300 to something else, I get a different, but still static, result.

[160211 20:35:29]: 239	
[160211 20:35:40]: 239	
[160211 20:35:45]: 239	

Kevin Hincker

unread,
Feb 12, 2016, 3:15:11 PM2/12/16
to Coronium IO
Chris answered:

It's due to the Lua VM stating up each request (I assume the Lua cache is off for development?).

In either event I usually run...

math.randomseed(os.time())

...before calling any math.random().  You really only need to call the "randomseed" method once in your cloud app. So you should be able to put it in the start up script, though that's not mandatory. Calling it repeatedly in your module code, is not an issue.
Reply all
Reply to author
Forward
0 new messages