String Array randomising algorithm in PowerBASIC.

Started by hutch--, December 20, 2009, 05:54:07 PM

Previous topic - Next topic

hutch--

I needed this algo for a DLL. It calles GetTickCount, converts it to a string, reverses the string then chops off the leading 6 digits which it converts back to an integer. It uses this integer as a seed for the random generator then does what is basically a card shuffle of the array members so that the array is effectively randomised.


' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

DECLARE FUNCTION Tick_Count LIB "KERNEL32.DLL" ALIAS "GetTickCount" () AS DWORD

SUB arr_rand(arr() as STRING)

    #REGISTER NONE

    LOCAL parr as DWORD                 ' array pointer
    LOCAL acnt as DWORD                 ' array member count
    LOCAL lbnd as DWORD                 ' lower bound of array
    LOCAL lcnt as DWORD
    LOCAL seed as LONG

    seed = val(left$(strreverse$(str$(Tick_Count)),6))

    lbnd = Lbound(arr())                ' get lowest array index
    parr = VarPtr(arr(lbnd))            ' get address of 1st array member
    acnt = ArrayAttr(arr(),4)           ' get the array member count

    ! mov edi, acnt
    ! mov esi, parr
    ! xor ebx, ebx
    ! mov lcnt, edi

  #align 4
  stlp:
    ! mov eax, seed
    ! test eax, &H80000000
    ! jz  nxt
    ! add eax, &H7FFFFFFF
  nxt:   
    ! xor edx, edx
    ! mov ecx, 127773
    ! div ecx
    ! mov ecx, eax
    ! mov eax, 16807
    ! mul edx
    ! mov edx, ecx
    ! mov ecx, eax
    ! mov eax, 2836
    ! mul edx
    ! sub ecx, eax
    ! xor edx, edx
    ! mov eax, ecx
    ! mov seed, ecx
    ! div lcnt
    ! mov eax, edx
    ! mov ecx, [esi+ebx*4]              ' get the incremental pointer
    ! mov edx, [esi+eax*4]              ' get the random pointer
    ! mov [esi+ebx*4], edx              ' write random pointer back to incremental location
    ! mov [esi+eax*4], ecx              ' write incremental pointer back to random location
    ! add ebx, 1                        ' increment the original pointer
    ! sub edi, 1                        ' decrement the loop counter
    ! jnz stlp

END SUB

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php