Jump to content

Seeding random generator


Rick
 Share

Recommended Posts

I don't see a seeding function in Math. How can I seed the random generator? Normally I would use:

 

math.randomseed(os.time)

 

but in sandbox mode you can't use 'os' object. Passing Time:GetCurrent() really doesn't help since it's the running time of the app. I'm stuck with the same numbers each run atm.

Link to comment
Share on other sites

You can use this unless you're doing randoms before rendering starts. There will be tiny variations in rendering speed.

first 5 will always be the same since GetSpeed is then 0 after first frame you will have randoms.

 

function Script:UpdateWorld()
   math.randomseed(Time:GetSpeed()*43758.5453)
   for x=0,4,1
   do
       System:Print(math.random())
   end
   System:Print(" ---- ")
end

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

LE really needs to expose some way to do a real seeding of it's random numbers

 

Aha..

math.randomseed(Time:Millisecs()) 

always gives you randoms even in Start().

 

Seems like Time:Millisecs() returns the system time in milliseconds, not apptime.

http://www.leadwerks.com/werkspace/page/api-reference/_/time/timemillisecs-r496

 

So there you go.. proper way smile.png

  • Upvote 1

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Interesting when I do it as the first line in Main.lua I don't get random numbers. Actually even in Start() I'm not getting random numbers.

 

function Script:Start()
math.randomseed(Time:Millisecs())

-- get random numbers and set the materials for our children
self.combo = {}
self.combo[1] = Math:Round(Math:Random(0, 9))
self.combo[2] = Math:Round(Math:Random(0, 9))
self.combo[3] = Math:Round(Math:Random(0, 9))
self.combo[4] = Math:Round(Math:Random(0, 9))
end

 

 

Looks like I'll have to use Lua's math.random() with this as LE's must use something different where doing Lua's seeding doesn't work. I figured LE's Math:Random() was just using Lua's math.random() but I guess not :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...