True random numbers
Monkey Forums/Monkey Programming/True random numbers
| ||
Do we have a randomiser such as what Blitz has: SeedRnd MilliSecs()? |
| ||
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. |
| ||
_GW, Sorry can you explain it's use?? |
| ||
Seed = Millisecs() |
| ||
_GW, Ok - thanks. Did have that actually. Still not sure it's totally working. Will do some investigation. [edit]Probably my code[/edit] |
| ||
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 |
| ||
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. |
| ||
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. |
| ||
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)???? |