### Author Topic: Anyone know how to recode this function for GLB  (Read 3415 times)

#### spicypixel

• Prof. Inline
• Posts: 646
• Pixel Artist
##### Anyone know how to recode this function for GLB
« on: 2015-Nov-06 »
This is a pseudo-random-number-generator which I'd really like to test out in GLB. My math knowledge is poor so thought I'd post this little function to see if anyone can answer my question regarding converting it to GLB. It's just the math section that needs doing really the results can be DEBUG output rather than printed (as per the example).

Code: (glbasic) [Select]
`//by default these are 0global BSDStateglobal MSState for i = 1 to 10    print randBSD()next for i = 1 to 10    print randMS()next function randBSD:    randBSD = (1103515245 * BSDState + 12345) mod (2 ^ 31)    BSDState = randBSDendfunction function randMS:    MSState = (214013 * MSState + 2531011) mod (2 ^ 31)    randMS = integer(MSState / 2 ^ 16)endfunction`

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

#### bigsofty

• Community Developer
• Prof. Inline
• Posts: 2721
##### Re: Anyone know how to recode this function for GLB
« Reply #1 on: 2015-Nov-06 »
Not tested but...

Code: (glbasic) [Select]
`GLOBAL BSDState#GLOBAL MSState%FUNCTION randBSD#:    BSDState = MOD( (1103515245 * BSDState + 12345), POW (2, 31) )    RETURN BSDStateENDFUNCTION FUNCTION randMS%:    MSState% = MOD ( (214013 * MSState + 2531011), POW (2, 31) ) / POW(2, 16)    RETURN MSState%ENDFUNCTION`
BTW POW(x,y) can be replaced by the POW(x,y) result. eg. POW (2, 16) could be replaced with 65536, maybe a compiler optimisation does this anyway?
« Last Edit: 2015-Nov-06 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

#### spicypixel

• Prof. Inline
• Posts: 646
• Pixel Artist
##### Re: Anyone know how to recode this function for GLB
« Reply #2 on: 2015-Nov-07 »
Good idea about the value of the POW to optimise calculations. I pretty much did what you have done there in my efforts by putting the MOD at the start, I think my only problem was not defining float or integer in the function name (I guess). Will test now my good friend. I believe the BSD pnrg is poor in comparison to the Microsoft method.

Results to be posted shortly

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

#### spicypixel

• Prof. Inline
• Posts: 646
• Pixel Artist
##### Re: Anyone know how to recode this function for GLB
« Reply #3 on: 2015-Nov-07 »
The BSD formula just blatantly didn't work and alternated between 0 and 12345. The MS routine gave good PRNGs but needs some sort of seeding as the sequence was identical each time. It was good to see the generation of random numbers from a small algo though

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

#### quangdx

• Mr. Polyvector
• Posts: 233
• work hard / play hard
##### Re: Anyone know how to recode this function for GLB
« Reply #4 on: 2015-Nov-07 »
I'm not sure how fast the MOD or the POW commands are, may not be so good for time critical sections of a game.
Asobi tech - the science of play.
Spare time indiegame developer.

#### kanonet

• Prof. Inline
• Posts: 1137
##### Re: Anyone know how to recode this function for GLB
« Reply #5 on: 2015-Nov-07 »
Im not sure about POW, but MOD should be no problem at all, its often included in CPU hardware.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

#### spicypixel

• Prof. Inline
• Posts: 646
• Pixel Artist
##### Re: Anyone know how to recode this function for GLB
« Reply #6 on: 2015-Nov-07 »
I replaced the POW's with integers and all was fine

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

#### sf-in-sf

• Mr. Drawsprite
• Posts: 93
• Artist F.P. Brixey
##### Re: Anyone know how to recode this function for GLB
« Reply #7 on: 2015-Nov-15 »
POW is normally for float numbers. better use ASL(1,n) than POW(2,n).
The  hex notatation 0x.... becomes easy with 2^n numbers:   2^4 = 0x10  2^8 = 0x100   2^12=1000
In the windos' accessories, the calculator has a decimal to/from hex converter, in the dev' mode.
On the day the atom is a cube I will start believing in the square pixel.