my next RNG .. CSG IV

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
dafhi
Posts: 1251
Joined: Jun 04, 2005 9:51

my next RNG .. CSG IV

Postby dafhi » Nov 02, 2018 2:33

CSG IV

Code: Select all

/'  Complex Sequence Generator IV -- 2019 Nov 13 - by dafhi

  highly configurable "RNG"

'/

#define unit  ubyte
#define u     as unit


function RNG(byref seed u) u
  #if 0
     '' xorshift
   
    seed xor= seed shr 3
    seed xor= seed shl 1
    seed xor= seed shr 5
  #else
     '' Complex Sequence Generator IV
     
    const shift_amount = len(unit)*2
   
    static u  seed2 = 0
   
    seed = 3 * seed + 32
    seed xor= seed shr shift_amount
    seed += seed2
    seed2 += 3
  #endif
  return seed
End function


var           pixel_size = 1

var           w = 256 * 3
var           h = 400 * 1


screenres w, h, 32


dim u         seed = 1

for y as integer = 0 to h step pixel_size
  var lenm = pixel_size - 1
  for x as integer = 0 to w-1 step pixel_size
    const COLOR_OFF = rgb(0,0,255)
    const COLOR_ON  = rgb(230,230,220)
    dim as ulong col = IIF( RNG(seed) < 2^(len(unit)*8)\2, _
      COLOR_OFF, COLOR_ON )
    line (x,y)-(x+lenm, y+lenm),col, bf
  next
next

sleep

previous ver.. CSG 3

Code: Select all

' Complex Sequence Generator 3 by dafhi - 2018 Nov 13
' high period, high configurability PRNG

' breezes PractRand to 32TB

type CSG3_LITERAL          as ubyte 'ulongint

dim shared as CSG3_LITERAL csg3_mul = 255 ' non-ideal  (32TB:  1645)
dim shared as CSG3_LITERAL csg3_add = 1   ' 2^bits co-prime?  (&H5851F42D4C957F2D)

dim shared as CSG3_LITERAL csg3_x   = any ' seeds
dim shared as CSG3_LITERAL csg3_a   = any '

const as integer shift_amount = ( len(csg3_x)*8 ) * (37 / 64) 'Recommended [max:38 min:8] / 64

function CSG3 as double
   
   csg3_a += csg3_add
   csg3_x *= csg3_mul
   csg3_x xor= csg3_x shr shift_amount
   csg3_x xor= csg3_a                  'or Add
   
   #if 0                               'option
    const as ulongint seed3 = any
    csg3_a -= (csg3_x = Seed3)
   #endif
   
   return csg3_x / 2 ^ (len(csg3_x)*8)
 
  /'  development history
   
    v1.  state = mul * (state shr j) + add
   
    v2.  a += c - (state = seed)
         state ror r
         state = mul * state + add
         state xor= a   '/

end function



' ---------

sub Main
 
 #define looper_2d  for y as integer = 0 to h-1: for x as integer = 0 to w-1

 const im_scalar   = 3

 const w = im_scalar * 210 '' 2 * 3 * 5 * 7
 const h = im_scalar * 143 '' 11 * 13

 screenres w,h,32

 const pel_size = 1

 #undef rnd
 #define rnd csg3

 for i as long = 0 to 9
  csg3_a = i
  csg3_x = 0
 
  looper_2d
   dim as long c = rnd * 256 - .5
   pset (x,y), rgb(c,c,c)
  next:  next
 
  if inkey=chr(27) then exit for
  sleep 1000
 next

 sleep

end sub

main
Last edited by dafhi on Sep 16, 2019 1:40, edited 2 times in total.
dafhi
Posts: 1251
Joined: Jun 04, 2005 9:51

Re: my next RNG .. CSG IV

Postby dafhi » Sep 13, 2019 14:44

new version

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 15 guests