RND behavior on the Caanoo

Previous topic - Next topic

erico

I have this game going and would like to have it running on desktops as well as caanoo and pandora.
Thing is, it has a set of RND commands to position some objects on screen.

While testing things yesterday night, I noticed everytime I run this on the caanoo, the RND is always the same. On desktops, no problems.
I remember back on the AMOS days, that in order to get to random numbers, you had to use a RANDOMIZE TIMER command, otherwise things would be similar to what I´m experiencing on caanoo.

So, is there such a way to do this?

kanonet

Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

erico

Hi, Kanonet,

according to the help files, it seems SEEDRND can be used with a seed to get a pattern of random numbers, which, if I understood this, is what I´m getting on the caanoo. (edit : I´m currently not using seedrnd)

It also states that:
"To make the random number generator generate a different random number sequence each time your program starts, GLBasic automatically calls SEEDRND(GETTIMERALL()) before your first line of code executes."

Maybe this is not getting called on the caanoo?

Mr.Tatoad´s book has a state on this as a note:
"if you find that you get the same numbers repetedly even with this command, you may need to find a better way to calculate the seed value"

It recommends, as an example, the use of SEEDRND GETTIMERALL().
It also states this will use the time the computer was turned on to generate this seed.

So, your hint sure got me some paths to analyse this, I will give a try later afternoon, and probably do a simple rnd code so I don´t have to test with the full game.

Thanks! :good:

Slydog

#3
Yes, what kanonet said!

I was just looking that command up when you posted!
I think the following was suggested to be a good method to seed it:

Code (glbasic) Select
SEEDRND MID$(PLATFORMINFO$("time"), 17, 2)

[Edit] Oops, just missed your next post.  I think GETTIMERALL() returns the time since your PROGRAM started (not your PC), so if you use that near the beginning of your program, it may return the same value every time.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

kanonet

Even right at the beginning of your program GETTIMERALL() should always give an other result, modern computers are so fast and modern operating systems do so much things at the same time in background - execution speed of your program will always differ a bit (not noticeable for you, but for SEEDRND it makes a big difference, even if it just differs 1/1000 ms!). So normally GETTIMERALL() should do the job on PC, but im not totally sure how its on such a small and less powerful machine like the caanoo, but i expect no problems, but better just try it. ;)
Of cause playing with Systemtime or other things (or mix it all up).

Btw is the caanoo linux based? So maybe /dev/random would be an idea?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

erico

Yep, linux based but "/dev/random" is alien to me as I have no knowledge on linux at all ;/

Wampus

I had this problem previously. The technique Slydog described using the time from PLATFORMINFO$ did the trick for me. This should produce a new result each game:-

Code (glbasic) Select
LOCAL time$[], ttemp#
SPLITSTR(PLATFORMINFO$("TIME"), time$[], ":- ")
ttemp# = INTEGER(time$[1] + time$[2] + time$[3] + time$[4] + time$[5] + GETTIMERALL())
SEEDRND ttemp

erico

Thanks a lot again guys, I´m sure to give it a go! :good:

One thing is that if I rebuild the same code , then run it on the caanoo again (without any SEEDRND) I get newer positions, but all subsequent runs show the same position. :S

I will check Slydog/Wampus recommendations now...

erico

#8
oh well :( no luck

I just got time to try slydog/wampus recommendation straight as it was posted, even changed the LOCAL to a GLOBAL one.
It behaves the same on caanoo.

Currently the game has no front end, as you run it, it loads things and jump straight into the game action.
Maybe if I add a joy button to start it may help(as timer may change)? damn it´s 2am, will give a go on that later as time shows up :'(


kanonet

So RND+SEEDRND does not work at all on caanoo? This should be a bug... Maybe you should not test it in your game, just create a test program, that just gives you a random number and exits. Does it work on PC, but not on caanoo? Like i said SEEDRND GETTIMERALL (if you want you can just use the decimals from GETTIMERALL, maybe this is even "more random") should perfectly work.

Quote from: erico on 2012-May-15
Yep, linux based but "/dev/random" is alien to me as I have no knowledge on linux at all ;/
/dev/random is a file that always contains highly random data, to get them you can read it, like any other file. It takes care, that is always save, so it can block/be a bit slow, that why its normally just used to set the seed of a prng. See here. In GLB this could work like this:
Code (glbasic) Select
LOCAL rv, random%
OPENFILE(random, "/dev/random",1)
READIEEE random, rv
CLOSEFILE random
SEEDRND rv

If you dont want to use RND/SEEDRND you can replace them with using /dev/random, but cuz of speed it would be better to just use /dev/urandom (less save, but faster), or maybe precache an amount of random numbers at program start. Of cause you have to think about which datatype you want to read (there are many READ... commands), and do conversations according to this.

Thats just in case you want to know it, like i said, it should not be needed and SEEDRND+RND should work perfectly... ;)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

This code is converted from the BlitzMax random routine, so should only really be used with your own code.  However, it could give you a few ideas in doing your own version :

Code (glbasic) Select
TYPE TRandom
rnd_state%=4660
RND_A=48271;RND_M=2147483647;RND_Q=44488;RND_R=3399

FUNCTION Initialise%:
self.SeedRandom(0)
RETURN TRUE
ENDFUNCTION

FUNCTION RndFloat:
self.rnd_state=self.RND_A*MOD(self.rnd_state,self.RND_Q)-self.RND_R*(self.rnd_state/self.RND_Q)
IF self.rnd_state<0 THEN self.rnd_state=self.rnd_state+self.RND_M
RETURN bAND(self.rnd_state,268435440) / 268435456.0 
ENDFUNCTION

FUNCTION RndDouble:
LOCAL TWO27 = 134217728.0
LOCAL TWO29 = 536870912.0
LOCAL r_lo%,r_hi%

self.rnd_state=self.RND_A*MOD(self.rnd_state,self.RND_Q)-self.RND_R*(self.rnd_state/self.RND_Q)
IF self.rnd_state<0 THEN self.rnd_state=self.rnd_state+self.RND_M
r_hi = bAND(self.rnd_state,536870908)

self.rnd_state=self.RND_A*MOD(self.rnd_state,self.RND_Q)-self.RND_R*(self.rnd_state/self.RND_Q)
IF self.rnd_state<0 THEN self.rnd_state=self.rnd_state+self.RND_M
r_lo = bAND(self.rnd_state,536870904)

RETURN (r_hi + r_lo/TWO27)/TWO29
ENDFUNCTION

FUNCTION RndStandard:min_value=0.0,max_value=1.0
IF max_value>min_value THEN RETURN RndDouble()*(max_value-min_value)+min_value
RETURN RndDouble()*(min_value-max_value)+max_value
ENDFUNCTION

FUNCTION Rand%:min_value%=0,max_value%=1
LOCAL range%=max_value-min_value

IF range%>0 THEN RETURN INTEGER(RndDouble()*(1+range%))+min_value%
RETURN INTEGER(RndDouble()*(1-range%) )+max_value%
ENDFUNCTION

FUNCTION SeedRandom%:seed%
self.rnd_state=bAND(seed,2147483647)
IF self.rnd_state=0 OR self.rnd_state=self.RND_M THEN self.rnd_state=GETTIMERALL()
ENDFUNCTION

FUNCTION RndSeed%:
RETURN self.rnd_state
ENDFUNCTION
ENDTYPE

LOCAL r AS TRandom

r.Initialise()
DEBUG r.RndStandard(5,7)+"\n"
DEBUG r.RndDouble()+"\n"
DEBUG r.Rand(5,7)+"\n"

kanonet

Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

erico

Hi guy,

Kanonet was right, I have tried a few tests here tonite.
SEEDRND (GETTIMERALL()) works now to generate random numbers correctly.

The thing is, it has to be on the program for the caanoo to use it, otherwise the pattern is always the same.
It is not needed when on PC and I guess it shouldn´t since the manual states this is hiddenly executed on the beginning of every code.

Another thing is that I wasen´t quite running the latest GLB (stupid me :-[)

One issue that I noticed is that I can´t read button 0 on the caanoo, the others are fine, but the A button won´t respond.

I thank everyone that helped me on this RND issue, it has been a great read and I learned a lot. :good: