Seed?
Monkey Forums/Monkey Programming/Seed?
| ||
In my OnCreate() method I'm putting in:Seed = Millisecs() for the first line. All my stuff appears in the same place though when I use Rnd to place things. This happening to anyone else? |
| ||
See below. |
| ||
And I see MilliSecs as both "MilliSecs" and "Millisecs" in the code and docs... So how do we randomly seed the Seed then? |
| ||
In the code sample you provided, isn't millisecs failing because it isn't inside an App instance (i.e. there's no "device" to query milllisecs from)? |
| ||
Here's my setup:Import mojo Class MyApp Extends App Method OnCreate() SetUpdateRate 60 Seed = Millisecs() End Method Method OnUpdate() End Method Method OnRender() End Method End Function Main() New MyApp End And it all still gets put in the same place. |
| ||
Does this work for you? |
| ||
Yep works fine. |
| ||
bear in mind that millisecs returns the number of milliseconds the app has been running for (which will always be 0 or near it if called in OnCreate) |
| ||
Right. So people are using Seed = Millisecs() to seed the random number generator...which actually is wrong, correct? Consider this below. You get the same set of numbers. So now my question is: How you do randomly seed the Seed? Is there a way to get the system time? That's how we did it in BMax. SeedRnd Millisecs(). Doesn't seem valid here in Monkeyland... Import mojo Global a=0 Class Game Extends App Method OnCreate() SetUpdateRate 60 Seed = Millisecs() End Method OnUpdate() If Not a a = 1 For Local i = 0 To 20 Print Rnd(0,100) Next Endif End Method OnRender() Cls(0, 0, 0) SetColor( 255, 255, 255 ) DrawText Millisecs(), 10, 10 End End Function Main:Int() New Game End Function |
| ||
a conundrum indeed, a Monkey Puzzle if you will. Perhaps a SystemMillisecs() command or something similar is needed? |
| ||
Roger that. I was thinking SystemTime() but either works! |
| ||
I first typed SystemTime() but the word millisecs made it look less ambiguous to me. |
| ||
Got this working in HTML5: create system.monkey (I created mine in modules/pert/) create a folder called native inside that, create a file called system.html5.js with the following: I might have a crack at other targets *EDIT* Actionscript (system.flash.as): |
| ||
What I do is just churn the CPU doing a lot of calculations for a bit, and then take Millisecs():Class MyApp extends App Method OnCreate() For Local c=1 to 100000000 Sqrt(c) Next Seed = Millisecs() End End |
| ||
Perturbatio - you can do something clever like this and avoid an external js file:Extern Function RealMillisecs:Int() = "(function(){return (new Date()).getTime();})" Public Function Main() Print RealMillisecs() End |
| ||
you can do something clever like this and avoid an external js file: I didn't know you could do that. Now if you could see your way to doing it for Android, XNA and GLFW I'll be more impressed :) |
| ||
Simon mentioned the same sort of thing as Perturbatio here: http://www.monkeycoder.co.nz/Community/posts.php?topic=185 |
| ||
Yeah but it's not a solution. We still need a cross-platform solution dopey. |
| ||
I was thinking it might be better to be added to App itself. But that means modding the mojo code |
| ||
This would be a very easy thing to write as just a separate module - if you're writing a new function called systemMillisecs(), it doesn't conflict with anything in mojo or the standard modules. |
| ||
So was there a fix for this for all platforms? |
| ||
RealMillisecs() was added to Diddy, which "should" work for all platforms - not tested on iOS or Mac. |
| ||
What's Diddy? |
| ||
What's Diddy? http://www.monkeycoder.co.nz/Community/posts.php?topic=583 |
| ||
This sounds like the kind of gotcha that should go in a 'porting from Blitz' guide. Certainly all my Blitz stuff is inited like this, and will now give you the same game every time. The systemMillisecs() sounds very useful, but we could also just have an srand() or something which seeds the random number generator to a random value; It doesn't have to be based on the system clock. |