Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Frank Brübach on August 29, 2024, 03:06:57 PM

Title: Rnd question again
Post by: Frank Brübach on August 29, 2024, 03:06:57 PM
Hello Charles..

How I can Change the seed value in my rnd example for different results Output? Like a randomize Timer or similar Effects?

  ' oxygen
  ' how I can change the seed value with a randomize timer
  ' for different output for every run ?
  '
  int seed=0x12345678
  '
  function Rnd() as float
  =======================
  '
  Static As float f, d=1/0x7fffffff
  mov eax,seed
  inc eax
  rol eax,13
  xor eax,0xdab5ca3a
  mov seed,eax
  push eax
  fild dword [esp]
  pop eax
  fmul dword d
  fstp dword f
  return f
  end function

Function Rnd2(sys z1,z2) as sys
    pushad
    sys  rand
    mov  eax,z2
    sub  eax,z1
    inc  eax
    imul edx,seed,0x8088405
    inc  edx
    mov  seed,edx
    mul  edx
    add  edx,z1
    mov  rand,edx
    popad
    Return rand
End Function


  'ABS RANDOMISER
  '
  function arnd() as float
  ========================
  '
  return Abs(Rnd)
  end function


  function irnd(int z1, z2) as int
  ================================
  '
  mov    eax,z2
  sub    eax,z1
  inc    eax
  imul   edx,Seed,0x8088405
  inc    edx
  mov    Seed,edx 'store new seed
  mul    edx 'multiply eax by edx
  return edx+z1
  end Function

'------------------------------------- //
' fixed values of seed produces same sequences as output

seed=12345 : print "neu: "+irnd 1,20 '8
seed=12345 : print "neu1: "+arnd  ' 0.275801...
seed=12345 : print "neu2: "+rnd2 1,16  '7
Title: Re: Rnd question again
Post by: Charles Pegge on August 29, 2024, 03:46:57 PM
Something like this:

uses corewin 'if not already included
...
seed=GetTickCount( )
...
Title: Re: Rnd question again
Post by: Frank Brübach on August 30, 2024, 09:26:26 AM
Thanks Charles.. does Work perfect :-)

'------------------------------------- //
'  values of seed produces now different sequences as output
'
seed=gettickcount() : print irnd 1,20 ' 6 ' 19 '12
print arnd '0.4769.. ' 0.7297..
print rnd2 1,16 ' 3 ' 15