Pseudo-Random Bit Sequence Generator
Algorithm creator(s)
Paul Dixon
PB author(s)
Paul Dixon
Description
Returns a random number from y to z from sequence x.
Note
As with all PRBS generators based on a tapped shift register, a seed of 0 is not allowed.
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-console-compiler/15491-randomize-seed?p=212524#post212524
See also
n/a
Source Code
Download source code file bitsequence.bas (Right-click -> "Save as ...")
$REGISTER NONE
GLOBAL r&&(),raddr&
FUNCTION rand(BYVAL n&,BYVAL First&,BYVAL Last&) AS LONG
REM calculate AND RETURN the NEXT RANDOM number IN the n&'th sequence
REM range of result is First& to Last&
REM raddr& is a global pointing to the first element of a global QUAD array which
REM holds the random number generator registers. This array can be re-seeded any time by just
REM assigning the element e.g. to seed generator 3 use r&&(3)=NewSeed.
REM Produces maximal length 33 BIT (2^33-1) sequence IN ANY number of independent sequences
REM The array and pointer are dim'd and made global elsewhere
REM As with all PRBS generators based on a tapped shift register, a seed of 0 is not allowed
!mov ebx,n& ;get which random generator to use
!shl ebx,3 ;*8 as there are 8 bytes used by each
!add ebx,raddr& ;add address of start of array
!mov edx,[ebx+4] ;from here on down it's complicated to explain but this calculates..
!shr edx,1 ;.. a 33 bit pseudo random binary sequence in the position just ..
!mov eax,[ebx] ;.. calculated in ebx.
!mov ecx,eax
!rcr ecx,1
!rcl edx,1
!mov [ebx+4],edx
!shl eax,12
!xor ecx,eax
!mov eax,ecx
!shr eax,20
!xor ecx,eax
!mov [ebx],ecx
!mov eax,ecx
!mov ecx,last& ;limit the range of the number to between First& and Last&
!add ecx,1
!sub ecx,First&
!div ecx
!add edx,First&
!mov function,edx
END FUNCTION
FUNCTION PBMAIN()
REM init the global array needed by the rand function
limit&=9 :REM want 10 independent RANDOM number sequences (0-9)
DIM r&&(limit&)
raddr&=VARPTR(r&&(0)):REM address of first RANDOM number
REM must make sure each seed isn't 0 or it'll not work
InitSeed&&=123456789
FOR r& = 0 TO limit&
r&&(r&)=InitSeed&&
NEXT
REM initialisation done, now use the function..
DO
CLS
PRINT "last number returned =";k&
PRINT
REM print out the contents of each random number generator
FOR r& = 0 TO limit&
PRINT r&,r&&(r&)
NEXT
PRINT
LINE INPUT"Next sequence to use?"; d$
q&=VAL(d$)
IF q&>limit& THEN q&=limit&
IF q&<0 THEN q&=0
k&=rand(q&,1,100):REM get the next random number in the q&'th sequence and limit it to 1-100
LOOP
END FUNCTION