High-Speed Pseudo-Random Number Generator
Algorithm creator(s)
Paul Dixon
PB author(s)
Paul Dixon
Description
A very fast implementation of a standard pseudo random binary sequence generator using a 33 bit feedback shift register.
Note
Not as random as other PSNGs but easily the fastest (at 26 million numbers/sec on a K6III-400 it is approx. 6 times faster than PBs built-in RND function). The generator uses 2 double word variables to store results (rng??? and rngh???) and takes just 13 opcodes inline, without looping. The sequence repeats after 2^33-1 numbers (8.5E9).
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23460-a-faster-pseudo-random-number-generator
See also
Source Code
Download source code file fastrnd.bas (Right-click -> "Save as ...")
FUNCTION PBMAIN()
REM fast PRBS generator for PB/CC. Produces maximal length
REM 33 bit (2^33-1) sequence but only gives 32 new bits each time.
REM rng??? is the 32 bit random number, set rng???=seed to seed the
REM number generator. Use RandomNumber???=rng??? to read the last
REM generated number. Run the routine (inline for speed) to generate
REM the next number. Uncomment the last line of the assembler code
REM to also write the new number direct to the variable where it's
REM needed.
rng???=1 :REM this is the random number generator value
rngh???=1 :REM this is the 33rd bit of the random number generator
limit&=100000000
PRINT "Running.."
DO
t!=TIMER
FOR g&=1 TO Limit&
!mov edx,rngh???
!shr edx,1
!mov eax,rng???
!mov ecx,eax
!rcr ecx,1
!rcl edx,1
!mov rngh???,edx
!shl eax,12
!xor ecx,eax
!mov eax,ecx
!shr eax,20
!xor ecx,eax
!mov rng???,ecx
'!mov VariableWhereItsNeeded&,ecx
NEXT
PRINT INT(Limit&/(TIMER-t!));" numbers/sec."
LOOP UNTIL INSTAT
END FUNCTION