Seed?

Monkey Forums/Monkey Programming/Seed?

Chroma(Posted 2011) [#1]
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?


Chroma(Posted 2011) [#2]
See below.


Chroma(Posted 2011) [#3]
And I see MilliSecs as both "MilliSecs" and "Millisecs" in the code and docs...

So how do we randomly seed the Seed then?


Perturbatio(Posted 2011) [#4]
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)?


Chroma(Posted 2011) [#5]
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.


Perturbatio(Posted 2011) [#6]
Does this work for you?



Chroma(Posted 2011) [#7]
Yep works fine.


Perturbatio(Posted 2011) [#8]
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)


Chroma(Posted 2011) [#9]
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



Perturbatio(Posted 2011) [#10]
a conundrum indeed, a Monkey Puzzle if you will.

Perhaps a SystemMillisecs() command or something similar is needed?


Chroma(Posted 2011) [#11]
Roger that. I was thinking SystemTime() but either works!


Perturbatio(Posted 2011) [#12]
I first typed SystemTime() but the word millisecs made it look less ambiguous to me.


Perturbatio(Posted 2011) [#13]
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):



Warpy(Posted 2011) [#14]
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



Warpy(Posted 2011) [#15]
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



Perturbatio(Posted 2011) [#16]
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 :)


dopeyrulz(Posted 2011) [#17]
Simon mentioned the same sort of thing as Perturbatio here:

http://www.monkeycoder.co.nz/Community/posts.php?topic=185


Chroma(Posted 2011) [#18]
Yeah but it's not a solution. We still need a cross-platform solution dopey.


Perturbatio(Posted 2011) [#19]
I was thinking it might be better to be added to App itself. But that means modding the mojo code


Warpy(Posted 2011) [#20]
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.


Chroma(Posted 2011) [#21]
So was there a fix for this for all platforms?


therevills(Posted 2011) [#22]
RealMillisecs() was added to Diddy, which "should" work for all platforms - not tested on iOS or Mac.


Chroma(Posted 2011) [#23]
What's Diddy?


Jesse(Posted 2011) [#24]

What's Diddy?


http://www.monkeycoder.co.nz/Community/posts.php?topic=583


CdrJameson(Posted 2011) [#25]
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.