True random numbers

Monkey Forums/Monkey Programming/True random numbers

dopeyrulz(Posted 2011) [#1]
Do we have a randomiser such as what Blitz has: SeedRnd MilliSecs()?


GW_(Posted 2011) [#2]
The global variable 'Seed' can be used.

Unfortunately the millisecs() function is tied to Mojo.
It should really be moved to the Monkey module so that its available everywhere.


dopeyrulz(Posted 2011) [#3]
_GW,

Sorry can you explain it's use??


GW_(Posted 2011) [#4]
Seed = Millisecs()


dopeyrulz(Posted 2011) [#5]
_GW,

Ok - thanks. Did have that actually.

Still not sure it's totally working. Will do some investigation.

[edit]Probably my code[/edit]


therevills(Posted 2011) [#6]
Heres some runnable code for you Matt:

Strict

Import mojo

Function Main:Int()
	New MyApp()
	Return 0
End Function

Class MyApp Extends App
	Field randNum:Int
	
	Method OnCreate:Int()
		Seed = Millisecs()
		SetUpdateRate 10
		Return 0
	End Method
	
	Method OnUpdate:Int()
		randNum = Rnd(-100,100)
		Return 0
	End Method
	
	Method OnRender:Int()
		Cls 32, 64, 12
		Scale 2,2
		DrawText randNum, 10,10
		Return 0
	End Method
	
End Class



skid(Posted 2011) [#7]
Unfortunately Millisecs will return 0 at the start of your program, so you need to wait for a key or mouse press before seeding the random number generator.


dopeyrulz(Posted 2011) [#8]
therevills,

Thanks i'll take a look.

Sorry got delayed by the kids playing Kinect Sports - great by the way for young kids (and older kids too).

Simon, ok will check that out.


therevills(Posted 2011) [#9]
Why when running this code, seed always changes?

Strict

Import mojo

Function Main:Int()
	New MyApp()
	Return 0
End Function

Class MyApp Extends App
	Field randNum:Int
	
	Method OnCreate:Int()	
		Seed = Millisecs()
		SetUpdateRate 1
		Return 0
	End Method
	
	Method OnUpdate:Int()
		randNum = Rnd(-100,100)
		Return 0
	End Method
	
	Method OnRender:Int()
		Cls 32, 64, 12
		Scale 3,3
		DrawText "Seed = "+Seed, 10,10		
		DrawText "Random Number = "+randNum, 10,30
		Return 0
	End Method
	
End Class


Even if I set the seed to by 1000, it changes on the Rnd(-100,100)????